Matlab - 返回行矩阵,其值不包括在最小和最大间隔中

时间:2014-01-10 10:48:16

标签: matlab

如何列出其值不包含在最小和最大间隔内的产品名称?

mat ={
'Name','Value','Min','Max';
'A1','26','1','25';
'A2','25','12','34';
'A3','25','','';
'A4','25','13','45';
'A5','25','','';
'A6','4','1','2'}

在这种情况下,我会返回以下值:

A1, (value 26 is not between 1 and 25)
A6,(value 4 is not between 1 and 2)

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

从您给定的矩阵开始,这将解决问题:

%// Extract and convert the values of interest
values = str2double(mat(2:end,2));

%// Extract & convert the bounds. 
%// Replace empties with appropriate values
mins = str2double(mat(2:end,3));    mins(isnan(mins)) = -inf;
maxs = str2double(mat(2:end,4));    maxs(isnan(maxs)) = +inf;

%// Extract the desired information. 
%// (Note that I ignore the headerline explicitly)
mat( [false; values < mins | values > maxs], 1 )

答案 1 :(得分:1)

您的示例mat无效,因此我将其解释为:

mat ={'A1', 26, 1   , 25;
      'A2', 25, 12  , 34;
      'A3', 25, -inf, inf;
      'A4', 25, 13  , 45;
      'A5', 25, -inf, inf;
      'A6', 4 , 1   , 2};

我使用-inf表示未知分钟,inf表示未知最大分数。然后你可以得到如下结果。

mat(~(([mat{:,2}] > [mat{:,3}]) & ([mat{:,2}] < [mat{:,4}])),1)

如果这不好,请发布一个我们可以重现的正确的完整示例mat


对于mat的新定义,您可以这样做:

mat ={
'A1','26','1','25';
'A2','25','12','34';
'A3','25','','';
'A4','25','13','45';
'A5','25','','';
'A6','4','1','2'};

请注意,我已从mat删除了标题行,因为这样会破坏内容

for row = 1:size(mat,1)
     if strcmp(mat{row, 3},'')
         mat{row,3} = '-inf';
     end
     if strcmp(mat{row,4},'')
         mat{row,4} = 'inf';
     end 
 end

 num = cellfun(@str2num,mat(:,2));
 mini = cellfun(@str2num,mat(:,3));
 maxi = cellfun(@str2num,mat(:,4));

 mat(((num < mini) | (num > maxi)), 1)