"Torsten" wrote in message <obll07$ml9$1@newscl01ah.mathworks.com>...
> "Christine Braganza" wrote in message <oblj0f$i9n$1@newscl01ah.mathworks.com>...
> > I'm trying to ask matlab if the (4,3) value of matrix G is equal to 5, 2 or 1.
> >
> > The matrix value of G(4,3)==2, but when I input (G(4,3)==(2||1||5)) it says the ans =0
That's correct. The expression (2 || 1 || 5) returns true, since 2 is non-zero. Since G(4, 3) is not zero or false, == returns false.
https://www.mathworks.com/help/matlab/ref/or.html
[Yes, I know that's the documentation for |, but the same idea applies to ||.]
> > How do I ask if the variable value is one of three values?
> >
> > I tried putting in G(4,3)==(4||5) and the answer was 0 (so untrue) but then I took away the brackets, G(4,3)==4||5 and the answer was 1(true)
That's correct. The == operator has higher precedence than the || operator. So that is equivalent to:
(G(4, 3) == 4) || 5
Regardless of what G(4, 3) is you're going to OR it with a non-zero value and that's going to return true.
https://www.mathworks.com/help/matlab/matlab_prog/operator-precedence.html
> > So I tried putting in G(4,3)==16||65 (these values don't even exist in my matrix, and it came out with the answer 1, true?! I don't know why though.
>
> (G(4,3)==2) || (G(4,3)==1) || (G(4,3)==5)
As a generalization, if you want to know if an element is one of a large set of potential values, you can use either ISMEMBER or a SWITCH/CASE construct.
https://www.mathworks.com/help/matlab/ref/ismember.html
ismember([3 pi], 1:10) % returns [true false] since 3 is in 1:10 but pi is not
https://www.mathworks.com/help/matlab/ref/switch.html
x = 3;
switch x
case {1, 4, 7, 10}
disp('remainder of 1');
case {2 5 8}
disp('remainder of 2');
case {3 6 9}
disp('remainder of 0');
otherwise
disp('x is not an integer value between 1 and 10 inclusive')
end
--
Steve Lord
slord@mathworks.com
To contact Technical Support, use the Contact Us link at the top of http://www.mathworks.com
> "Christine Braganza" wrote in message <oblj0f$i9n$1@newscl01ah.mathworks.com>...
> > I'm trying to ask matlab if the (4,3) value of matrix G is equal to 5, 2 or 1.
> >
> > The matrix value of G(4,3)==2, but when I input (G(4,3)==(2||1||5)) it says the ans =0
That's correct. The expression (2 || 1 || 5) returns true, since 2 is non-zero. Since G(4, 3) is not zero or false, == returns false.
https://www.mathworks.com/help/matlab/ref/or.html
[Yes, I know that's the documentation for |, but the same idea applies to ||.]
> > How do I ask if the variable value is one of three values?
> >
> > I tried putting in G(4,3)==(4||5) and the answer was 0 (so untrue) but then I took away the brackets, G(4,3)==4||5 and the answer was 1(true)
That's correct. The == operator has higher precedence than the || operator. So that is equivalent to:
(G(4, 3) == 4) || 5
Regardless of what G(4, 3) is you're going to OR it with a non-zero value and that's going to return true.
https://www.mathworks.com/help/matlab/matlab_prog/operator-precedence.html
> > So I tried putting in G(4,3)==16||65 (these values don't even exist in my matrix, and it came out with the answer 1, true?! I don't know why though.
>
> (G(4,3)==2) || (G(4,3)==1) || (G(4,3)==5)
As a generalization, if you want to know if an element is one of a large set of potential values, you can use either ISMEMBER or a SWITCH/CASE construct.
https://www.mathworks.com/help/matlab/ref/ismember.html
ismember([3 pi], 1:10) % returns [true false] since 3 is in 1:10 but pi is not
https://www.mathworks.com/help/matlab/ref/switch.html
x = 3;
switch x
case {1, 4, 7, 10}
disp('remainder of 1');
case {2 5 8}
disp('remainder of 2');
case {3 6 9}
disp('remainder of 0');
otherwise
disp('x is not an integer value between 1 and 10 inclusive')
end
--
Steve Lord
slord@mathworks.com
To contact Technical Support, use the Contact Us link at the top of http://www.mathworks.com