Amira Akra
Amira Akra

Reputation: 163

Remove Row under a condition

I have a matrix X=[0 1 0 0;0 0 0 1;1 0 0 1] and i want to remove the rows that have more than one '1' in it. I already know that sum(X~=0,2)removes the row will all zeros. Is there a way to do it without a loop?

Upvotes: 0

Views: 52

Answers (1)

Eugene Sh.
Eugene Sh.

Reputation: 18321

This code will do:

 X( sum(X') <= 1 , :)

sum(X') <= 1 will return boolean vector containing 1's at indices of rows with one or zero ones.

Update: Thanks, commenters. Instead of sum(X'), sum(X,2) should be used to avoid problems with single row matrices.

Upvotes: 3

Related Questions