TimeIsNear
TimeIsNear

Reputation: 755

Matlab - Return rows matrix which values are not included in the interval min and max

How do I list the products names whose value is not included in the interval min and max?

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'}

In this case I would return the following values:

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

Thank you very much in advance for your help!

Upvotes: 0

Views: 62

Answers (2)

Rody Oldenhuis
Rody Oldenhuis

Reputation: 38042

Starting from your given matrix, this will do the trick:

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

Upvotes: 2

Dan
Dan

Reputation: 45752

Your example mat is invalid so I've interpreted it like this:

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};

I've used -inf for unknown mins and inf for unknown maxs. Then you can get your result as follows.

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

If this is no good, then please post a proper complete example mat that we can reproduce.


For your new definition of mat you can do this:

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

note that I have removed your header line from mat because that will break things

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)

Upvotes: 1

Related Questions