MATLAB Answers

How to use use = logical operator on multiple values?

3 views (last 30 days)
Koki
Koki on 21 Apr 2020
Answered: Mohammad Sami on 21 Apr 2020
For example if I had vector A = [1,5,8,2,6,4,3];
And I want to check if A equal to 1st value, 2nd value or 4th value
istead of doing:
A1 = (A == A(1))| (A == A(2)) | (A == A(4))
Is there a shorter way, so like: A1 == A(1,2,4)
idk how to explain soz

  2 Comments

Ameer Hamza
Ameer Hamza on 21 Apr 2020
Koki, can you explain with a clear example. What does the statement "A equal to 1st value, 2nd value or 4th value" means? A is a vector, and its elements are scalar? How do you want to compare?

Sign in to comment.

Accepted Answer

Mohammad Sami
Mohammad Sami on 21 Apr 2020
As suggested by stephen, you can simply use ismember
A1 = ismember(A,A([1 2 4]));

  0 Comments

Sign in to comment.

More Answers (0)