Using AND Operator in “if” statements
Show older comments
Hi, When I type the following code: if size([1 2 3])==size([4 5 6]) & size([4 5 6])==size([7 8 9]) 'yes' else 'no' end MATLAB Code Analyzer issues this warning message: "When both arguments are numeric scalars, consider replacing & with && for performance." So, I use && instead of &: if size([1 2 3])==size([4 5 6]) && size([4 5 6])==size([7 8 9]) 'yes' else 'no' end But when I run the updated script, MATLAB displays an error message in the Command Window: ??? Operands to the || and && operators must be convertible to logical scalar values. What can I do to fix this? Thanks in advance. Andrew DeYoung Carnegie Mellon University
7 Comments
Sean de Wolski
on 10 May 2011
Your question is well written, you provide: an example, what you've tried, and what you want - everything we typically request. Good job.
Andrew Newell
on 10 May 2011
I second that!
Matt Fig
on 10 May 2011
This warning from MATLAB is silly. IF short circuits no matter whether you use & or &&. For example,
if false & error('Ooops')
disp('Here')
end
The only difference left is that & will accept vectors and && wants scalars. Note also that when using vectors, all elements must evaluate to true to pass the conditional.
You could also put this on the same line as the IF: %#ok
That will make M-Lint be quiet...
Matt Tearle
on 10 May 2011
Discovered lurking in the doc for IF & "programming fundamentals -> basic program components -> operators":
"When used in the context of an if or while expression, and only in this context, the element-wise | and & operators use short-circuiting in evaluating their expressions. That is, A|B and A&B ignore the second operand, B, if the first operand, A, is sufficient to determine the result.
So, although the statement 1|[] evaluates to false, the same statement evaluates to true when used in either an if or while expression"
The idea of the warning (as I understand the reasoning) is that relying on conversion from array to scalar in an if/while is a bad idea. Eg "if x~=y" probably doesn't do what you'd expect when x and y are vectors. So the warning is essentially saying "hey, use && to be sure you're getting the behavior you want/expect". If you use &, your code might run, but not give intended behavior.
Matt Fig
on 10 May 2011
But of course the same thing could be said for using any arrays with IF. All the time Answers we see folks puzzled by the output of their code when using vectors with IF. That all-or-none behavior (which essentially is scalar) with arrays gets a lot of people. Chaining this behavior with & makes little difference in this regard....
So I still say the warning is silly, unless M-Lint also gives a warning about IF with vector args (it doesn't).
Matt Tearle
on 11 May 2011
You're right about IF and vectors, but the Code Analyzer doesn't necessarily know which variables are vectors and which aren't. The & operator is one instance where it can give a message without having to determine that. The str2num vs str2double message is another example. It's vaguely annoying to get a warning, but the Analyzer's just hedging its bets.
[BTW, I'm just passing on the "official" answer here. I was about to submit a request saying, basically, exactly what you've said. But I found an existing discussion, and I've paraphrased the end decision. I'm sure there's more to it than I can skim and pass along.]
Jenny
on 2 Aug 2016
Matt, I still use my notes from your class, and thanks for the help in this question. I needed to get this right.
Accepted Answer
More Answers (1)
Sean de Wolski
on 10 May 2011
As an addendum to Andrew's thorough and good solution, you can check the sizes directly:
if(isequal(size([1 2 3]),size([4 5 6])))
disp('yes')
else
disp('no')
end
This will fail if the sizes are not the same but the number of elements (numel) is:
if(isequal(size([1 2 3]),size([4; 5; 6])))
disp('yes')
else
disp('no')
end
3 Comments
Andrew
on 10 May 2011
Matt Tearle
on 10 May 2011
Another benefit to isequal is that it won't throw an error message in situations where == will (it will just return false). For example
if size(rand(2))==size(rand(3,2,2))
disp('yes')
else
disp('no')
end
will fail because size will return a 3-element vector for a 3-D array. However
if isequal(size(rand(2)),size(rand(3,2,2)))
disp('yes')
else
disp('no')
end
works fine.
Andrew Newell
on 10 May 2011
Good point.
Categories
Find more on Loops and Conditional Statements 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!