How can I compare the values of different columns of the same matrix
Show older comments
I want to compare the values of the vector/matrix ZL with eachother. This is my code. It always displays false even if the values are equal
ZL=input('ZA ZB ZC ','s');
if ZL(1,1)==ZL(1,2)==ZL(1,3)
display('True')
else display ('false')
end
Accepted Answer
More Answers (2)
Image Analyst
on 5 Apr 2019
Try this:
if ZL(1,1) == ZL(1,2) && ZL(1,1) == ZL(1,3)
etc.
madhan ravi
on 6 Apr 2019
Edited: madhan ravi
on 6 Apr 2019
According to your claim it always returns 0 is because
let's break it down:
1) ZL(1,1)==ZL(1,2) will return a logical array maybe one 1 or 0.
2) You then compare that result with the third element which ZL(1,3) which is not 0 or 1 according to your claim when comapred to 0 or 1 it is not equal so returning false at the end.
So instead:
The right way is to use isequal() because if you have ZL to have more values, the approach of yours would be tiresome.
Remove 's' from your first line:
ZL=input('ZA ZB ZC '); % removed s, assuming your values are numeric
z = mat2cell(ZL,size(ZL,1),ones(1,size(ZL,2)));
isequal(z{:}) % just use a comma separated list to verify if each columns are equal, 0 means false 1 means true
2 Comments
Arouj
on 7 Apr 2019
madhan ravi
on 7 Apr 2019
Edited: madhan ravi
on 7 Apr 2019
It is not useful by saying "giving me error" what error did you get ? Post the complete error message you get (everything in red) .The input should be
[1-1*i,2-2*i,3] % should be a valid MATLAB syntax
Categories
Find more on String Parsing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!