Check whether values of a certain column of a matrix is between two values

2 views (last 30 days)
%{
I have a BUS matrix with 6 rows, column 12 is the max value and column 13 is the minimum value.
I want to check if column 8 values are between column 12 and 13 values.
Now, I know that the answer of volmax are all greater than 0 but some values of volmin are less than 0.
For some reason, my code display Yes which means that volmax and volmin are greater than or equal to zero.
But I know this is not true. My guess is that my code is comparing only the first row and it ignore the rest of the rows
Can you help me to figure this out?
}%
volmax= BUS(:,12)- BUS(:,8) % we are good if volmax >=0
volmin= BUS(:,8)- BUS(:,13) % we are good if volmin >=0
if volmax<0 | volmin <0
disp('NO')
else
disp('YES')
end
end

Accepted Answer

the cyclist
the cyclist on 30 Nov 2021
OK, still trying to understand everything, but I think this does what you want:
% Sample input
BUS(:, 8) = [2;2;2];
BUS(:,12) = [3;3;3];
BUS(:,13) = [1;1;1];
volmax = BUS(:,12)- BUS(:,8); % we are good if volmax >=0
volmin = BUS(:,8)- BUS(:,13); % we are good if volmin >=0
if any((volmin<0)|(volmax<0))
disp('NO')
else
disp('YES')
end
Change the inputs around, and see if it is correct. If this is NOT correct, then please
  • give an example of inputs that give INCORRECT output, and explain why
  2 Comments
Steven Shaaya
Steven Shaaya on 30 Nov 2021
Thank you again. I figured out the answer to my question which is adding zeros to the matrix in order to make them of the same size.
If you have better method, I would like to hear from you.

Sign in to comment.

More Answers (1)

the cyclist
the cyclist on 30 Nov 2021
I think you intended
volmax<0 & volmin <0
rather than
volmax<0 | volmin <0
because you want to meet both conditions ("AND"), not just either condition ("OR").
Also, be aware that for vector input V, you will only enter the if statement
if V
...
end
if all elements of the vector V are true. (It is not some kind of implicit loop that works down the vector.)
Note that the expression
volmax<0 | volmin <0
(not using the if structure at all) will be a logical vector, with TRUE/FALSE entries corresponding to each row.
  4 Comments
Steven Shaaya
Steven Shaaya on 30 Nov 2021
I want just a single "YES" / "NO", based on whether all rows are ok, all values of column 8 should be between the values of columns 12 & 13.
So based on your assumption, it should display 'no'.
For example, I can try
if volmax(1:6,:) <0 | volmax(2:6,:) <0 |volmax(3:6,:) ...........ect
but I do not see this efficient because I will need about 12 OR only for this example and my next example will have about 30 OR
that why I am trying to find another method
Thank you

Sign in to comment.

Tags

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!