MATLAB Answers

Using AND Operator in “if” statements

3,261 ビュー (過去 30 日間)
Andrew
Andrew 2011 年 5 月 10 日
編集済み: MathWorks Support Team 2020 年 9 月 2 日
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 件のコメント

表示 4 件の古いコメント
Matt Fig
Matt Fig 2011 年 5 月 10 日
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
Matt Tearle 2011 年 5 月 11 日
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
Jenny 2016 年 8 月 2 日
Matt, I still use my notes from your class, and thanks for the help in this question. I needed to get this right.

サインインしてコメントする。

採用された回答

Andrew Newell
Andrew Newell 2011 年 5 月 10 日
The problem is that size returns a vector:
size([1 2 3])
ans =
1 3
Instead, use numel:
if numel([1 2 3])==numel([4 5 6]) && numel([4 5 6])==numel([7 8 9])
disp('yes')
else
disp('no')
end
Or you could use all(size([1 2 3])==size([4 5 6]) etc.
I have also put in the disp commands to take care of the other warnings.

  1 件のコメント

Andrew
Andrew 2011 年 5 月 10 日
Thanks!

サインインしてコメントする。

その他の回答 (1 件)

Sean de Wolski
Sean de Wolski 2011 年 5 月 10 日
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 件のコメント

Andrew
Andrew 2011 年 5 月 10 日
Thank you!
Matt Tearle
Matt Tearle 2011 年 5 月 10 日
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
Andrew Newell 2011 年 5 月 10 日
Good point.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by