Matlab - 返回行矩阵,其值不包括在间隔 min 和 max 中

  • 本文关键字:min max 不包括 返回 Matlab 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)

提前非常感谢您的帮助!

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

%// 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 )

您的示例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)

最新更新