Logic conditions are not equal in if statements

3 views (last 30 days)
Hello.
I am having problems understanding logical expressions and how they differ sometimes, or why certain things work or not.
% Method 1
if (size(LPData.maty)==size(LPData.matx))
disp('A')
else
disp('B')
error('Error occured.\Vital Matrices matx and maty do not agree.\nCheck for different number of columns.')
end
% Method 2
if ~(size(LPData.maty)==size(LPData.matx))
disp('C')
error('Error occured.\Vital Matrices matx and maty do not agree.\nCheck for different number of columns.')
end
Maty and matx are matrices of different sizes in this case. I understand why, in the first case, this should trigger the error message, because the two sides of the logical array (size(LPData.maty)==size(LPData.matx)) are not equal. Hence, the statement under "else" is triggered.
But why doesn't the same happen in the second case? As I understand it, the condition is not fulfilled, therefore wrong, and the path under "C" should be triggered.
I know I could just use method 1 to achieve what I want, but I'd like to get rid of the imo unhandy way of using else, instead of just using hethod 2.
Would method 2 even be possible in theory?
Could someone clarify this?
Thank you.
Stay healthy,
Claudius Appel

Accepted Answer

madhan ravi
madhan ravi on 3 Jun 2020
"if expression, statements, end evaluates an expression, and executes a group of statements when the expression is true. An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric). Otherwise, the expression is false.”
From the documentation.
  2 Comments
Stephen23
Stephen23 on 29 Aug 2020
Edited: Stephen23 on 29 Aug 2020
"So, as I understand it, you can't filter for false statements directly, since a false statement is zero."
No, that is not what it means. You are conflating the truthiness of the elements with the truthiness of the expression.
That line from the documentation explains that all elements must be true in order for the expression to be considered true. Lets take a simple example of two different matrices with sizes 2x3 and 2x5, then your code does this:
~([2,3]==[2,5])
~[true,false]
[false,true]
Are all of the elements true ? No, they are not, so the expression is not considered to be true.
If the two matrices (or arrays with the same number of dimensions) share one or more dimensions with the same size, then your expression will be considered false. Only in the case that all dimensions differ will your expression will be considered true, because the final logical vector all of its elements will be true (and so the expression will be considered true, just as the documentation states). E.g. 2x3 and 4x5 matrices:
~([2,3]==[4,5])
~[false,false]
[true,true]
Tip: I suspect that you actually wanted to check if the array sizes were not the same, in which case the most robust approach is to use isequal like this:
~isequal(size(A),size(B))
Note that your code will throw an error if the two arrays have a different number of dimensions.

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!