if statement not working
Show older comments
Hi, my following piece of code does not work as it expected to and I don't know how to fix it yet. Any suggestions? Thanks!
A = [5;5;5;3;3;2];
B = [10;4;10;10;4;4];
if A == 5
plot(A,B,'rx')
elseif A == 3
hold on
plot(A,B,'bx')
else
hold on
plot(A,B,'gx')
end
2 Comments
Stephen23
on 5 Apr 2019
"if statement not working"
Did you read the IF documentation (to know how it works with non-scalar conditions), before deciding that it is "not working" ?
@Stephen I think Matlab should throw a warning on non-scalar inputs, maybe even an error. I've many people surprised by the implicit call to all that happens here.
%actually, if does this:
if ~isempty(expression) && all(expression)
Accepted Answer
More Answers (0)
Categories
Find more on Annotations 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!