J.Smith
J.Smith

Reputation: 335

Check if set values exists in Matrix

The matrix <1x500> contains different values, I am trying to do an if-statement to check if there is 3 different values, lets say it somewhere must contain 30, 40 and 50 in order to evaluate to true. They don't have to come in order.

I tried:

if all any(val == 30) && any(val == 40) && any(val == 50)
   do stuff

But it's not really working as intended, it seems it evaluate to true if only one of the values exists.

Upvotes: 1

Views: 421

Answers (1)

Suever
Suever

Reputation: 65430

You have an extra and unnecessary all in there. You can use simply

if any(val == 30) && any(val == 40) && any(val == 50)

Alternately, you could use ismember to simultaneously check for all values in the input.

if ismember([30 40 50], val)

Upvotes: 2

Related Questions