How can I compare the values of different columns of the same matrix

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

Basically, the function input with the argument 's' returns a char array (string), you need to convert it into vector of numbers, like this:
ZL=input('ZA ZB ZC ','s'); % this is returning a char array
ZL = sscanf(ZL,'%f')'; % add this line to break the char array into vector of numbers
% the transpose (') at the end is to get a row vector instead of column vector
if ZL(1,1)==ZL(1,2)==ZL(1,3)
display('True')
else display ('false')
end

9 Comments

ZL(1,1)==ZL(1,2)==ZL(1,3) is not valid syntax
True ... Thanks for pointing this out. Here is the corrected code:
ZL=input('ZA ZB ZC ','s'); % this is returning a char array
ZL = sscanf(ZL,'%f')'; % add this line to break the char array into vector of numbers
% the transpose (') at the end is to get a row vector instead of column vector
if ZL(1,1)==ZL(1,2) && ZL(1,2)==ZL(1,3)
display('True')
else
display ('false')
end
The inputs to ZL can be complex numbers thats why I added the 's'. Can u suggest any method in which I can input the complex numbers and compare them?
You can remove the 's' and enter the data as a valid matlab format (include the brakets for vector/matrix input) e.g. [1+2i, 3+4i, 5+6i] will input a vector of three complex numbers. Of course in this case you remove the sscanf function.
ZL=input('[ZA ZB ZC] ');
if ZL(1,1)==ZL(1,2) && ZL(1,2)==ZL(1,3)
display('True')
else
display('false')
end
There is no need for an if/else statement here.
It depends on how the rest of Arouj's code is.
If ZL has for instance more elements it would'nt be better to compare each element, better would be to create a comma separated list to compare all at once.
Arouj, I removed your flag saying "Answer" on this post since flags are just supposed to let moderators know that they need to check into something weird with the post (like the original poster deleted everything or more info is needed or similar). Just marking the answer as "Accepted" and perhaps a comment saying thanks is they way to go.
By the way, since I gave this answer first, prior to notifying Sawas who changed it to match mine, could you at least Vote for mine? Don't worry, it won't take away from his reputation points. Voting for and accepting answers gives posters "reputation points", so thanks for doing both.

Sign in to comment.

More Answers (2)

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

This approach is giving me error
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

Sign in to comment.

Products

Asked:

on 5 Apr 2019

Commented:

on 7 Apr 2019

Community Treasure Hunt

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

Start Hunting!