Is equality == available in Matlab 2017 ?

1 view (last 30 days)
JGUIS
JGUIS on 21 Aug 2018
Answered: Chad Greene on 21 Aug 2018
Hello, I use Matlab 2017 R2017b and I would like to establish an equality of two vectors of this kind :
if true
A = [1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4];
B = [1 1 1 2 2 2 3 3 3 4 4 4];
A == B
end
I would like to obtain a vector as result but ''=='' is not recognize. Is equality not exist in Matlab version 2017? If yes, how can I exactly replace that to have the same result that furnished ''==''
  1 Comment
Stephen23
Stephen23 on 21 Aug 2018
Edited: Stephen23 on 21 Aug 2018
"Is equality not exist in Matlab version 2017"
== exists for all MATLAB versions since atleast the first PC version of 1985.
"I would like to obtain a vector as result but ''=='' is not recognize."
You don't define what "equality" means for vectors of two different sizes. What size do you expect the output vector to be?

Sign in to comment.

Answers (3)

Geoff Hayes
Geoff Hayes on 21 Aug 2018
JGUIS - equality does exist in MATLAB, see Determine equality for details. With your above example, you are probably observing the
Error using ==
Matrix dimensions must agree.
error because your two arrays are of different dimensions and so cannot possibly be equal.
I suspect that you are looking for a different type of equality (perhaps they have the same elements, same order, etc.). Please clarify.
  2 Comments
ARN
ARN on 21 Aug 2018
Matrix dimensions should be equal to use this operator and i am using "==" in Matlab 2017 and its working. Make sure the two vectors/matrices you are comparing must be of same size. You can also see isequal if it fits in your code
JGUI
JGUI on 21 Aug 2018
Thank a lot for your answer, In fact, it was an error of inattention about the size of the matrix I try this and I obtain what i was looking for : a binary vector 1x19
if true
W = [1 1 1 1 1 2 2 2 2 3 3 3 3 3 4 4 4 4 4];
for i = 1 : 4
wx_ind = W == i;
end

Sign in to comment.


Steven Lord
Steven Lord on 21 Aug 2018
I'm fairly certain the == operator has been part of MATLAB since Cleve's original version. [If not, it was introduced not long after that.] It is certainly present in release R2017b. The problem you receive is not that == is undefined, it's that you're trying to compare arrays of different sizes.
If you wanted a matrix as a result, with a number of rows equal to the number of elements in A and a number of columns equal to the number of elements in B, that's possible thanks to implicit expansion.
A = [1 2 3 4];
B = [3 1];
A.' == B
If you wanted to know if the elements in A are present in B or vice versa, take a look at the ismember function.
If neither of those are what you want, please explain in more detail what you expect the result of A == B to be (what size should it be?) and how you would compute it by hand and we may be able to help you determine how to compute it in MATLAB.

Chad Greene
Chad Greene on 21 Aug 2018
I think you want isequal.

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!