find rows with all but one nonzero element

17 views (last 30 days)
hi,
after some calculation i get a matrix for example like this-
A=[1 0 1 1; 0 1 1 0; 1 0 1 0; 0 0 1 0]
now from A i want to output the row with all but one nonzero element
B=[0 0 1 0]
which will be the 4th row in this example.
how do i do it?

Accepted Answer

jonas
jonas on 21 May 2018
Here's one way:
A(sum(A,2)==1,:)
  3 Comments
jonas
jonas on 21 May 2018
No problemo, just add this first line to the code:
target=min(sum(A,2))
A(sum(A,2)==target,:)
if you want to know the row index, then just type:
sum(A,2)==target

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!