user537670
user537670

Reputation: 821

Matlab matrix ones elements checking

Suppose I have a vector like x = [1 1 1 1 1 1].

Now I have to write an if condition, where I have to check whether x contains all its elements as ones or not. How can this be done?

I searched in matlab's help, but couldn't find any direct "command" to check such a condition. Also the size of my vector varies, so can't use something like x(1,1) == 1 && x(2,1) ..... condition.

Upvotes: 0

Views: 1412

Answers (1)

mtrw
mtrw

Reputation: 35088

all(x == 1) will return 1 if all the members are 1.

If you'd rather check the reverse, use any(x ~= 1).

Upvotes: 3

Related Questions