find rows with all but one nonzero element

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?

 採用された回答

jonas
jonas 2018 年 5 月 21 日

0 投票

Here's one way:
A(sum(A,2)==1,:)

3 件のコメント

Muhammad Ridwanur Rahim
Muhammad Ridwanur Rahim 2018 年 5 月 21 日
編集済み: Muhammad Ridwanur Rahim 2018 年 5 月 21 日
@jonas, thank you. can you also tell me based on the given matrix, how can i find the row with the highest number of zeros and output that entire row?
and also, the output if there are multiple rows with the same number of zeros.
thank you.
jonas
jonas 2018 年 5 月 21 日
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
Muhammad Ridwanur Rahim
Muhammad Ridwanur Rahim 2018 年 5 月 21 日
thank you

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by