user1508419
user1508419

Reputation: 333

check if a column is composed of ones

does it exist a function built-in in matlab that checks if a column is all composed of ones? If it doesn't exist, there are some ways to build a function that work to achieve that porpouse?

Upvotes: 1

Views: 58

Answers (3)

Jacob
Jacob

Reputation: 34601

all(A==1) should return true if it's composed of only 1s. Note that if you have any floating point precision errors, you should use all( abs(A-1) < eps ).

Upvotes: 6

AGS
AGS

Reputation: 14498

If A is a column vector:

A=[1 1 1 1]';

You could check like this:

sum(A==1)==length(A)
ans =

 1

Upvotes: 0

angainor
angainor

Reputation: 11810

You can compare all entries of a column to 1 and sum the result

if sum(A(:,1)~=1)==0
    % all ones
else
    % not all ones
end

Upvotes: 0

Related Questions