MATLAB: How to compare the user input value in a given matrix.

matrix array

b=[ 10 32 54 76 -25
20 60 100 140 -51
45 135 -135 -45 59]
b1=input(' b1= ');
b2=input('b2= ');
b3=input('b3= ');
b4=input('b4= ');
if (b1==b(1)||b(6)||b(11))&&(b2==b(2)||b(7)||b(12))&&(b3==b(3)||b(8)||b(13))&&(b4==b(4)||b(9)||b(14))
disp('MATCH FOUND');
else
disp('MATCH NOT FOUND');
end

Best Answer

MATLAB does not have transitive "==" operators. (I don't know of any programming languages which do.) The section of code
(b1==b(1)||b(6)||b(11))
does not mean "(b1 is equal to b(1)), or (b1 is equal to b(6)), or (b1 is equal to b(11))". Each of the sections between the "||" is evaluated separately, so instead it means "(b1 is equal to b(1)), or (b(6) is true), or (b(11) is true)" and in MATLAB, a value is true if it is not 0. (Except that testing NaN in that syntax generates an error.)
If you want to test b1 against those three values, you need to write the "==" tests out,
b1 == b(1) || b1 == b(6) || b1 == b(11)
or you can use
ismember(b1, [b(1), b(6), b(11)])
or more compactly
ismember(b1, b([1 6 11]))