- give an example of inputs that give INCORRECT output, and explain why
Check whether values of a certain column of a matrix is between two values
2 views (last 30 days)
Show older comments
%{
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
0 Comments
Accepted Answer
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
More Answers (1)
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.
See Also
Categories
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!