Logic conditions are not equal in if statements

3 views (last 30 days)
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))
error('Error occured.\Vital Matrices matx and maty do not agree.\nCheck for different number of columns.')
% Method 2
if ~(size(LPData.maty)==size(LPData.matx))
error('Error occured.\Vital Matrices matx and maty do not agree.\nCheck for different number of columns.')
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.
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:
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:
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:
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)


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


Community Treasure Hunt

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

Start Hunting!