Finding row index in a matrix in which the sum of the elements is greater than 1

6 ビュー (過去 30 日間)
I have the following matrix:
S=[1,0,0;0,1,0;1,1,0;0,1,1;0,0,1]
I want to find row indexes in which the sum of the elements is greater than 1.
How can I do this?
Thanks

採用された回答

John D'Errico
John D'Errico 2023 年 2 月 17 日
編集済み: John D'Errico 2023 年 2 月 17 日
Just do EXACTLY what you said. I'll break it down into pieces.
S=[1,0,0;0,1,0;1,1,0;0,1,1;0,0,1]
S = 5×3
1 0 0 0 1 0 1 1 0 0 1 1 0 0 1
sum(S,2) % sum of the rows
ans = 5×1
1 1 2 2 1
sum(S,2) > 1 % testing if they exceed 1
ans = 5×1 logical array
0 0 1 1 0
find(sum(S,2) > 1) % which rows satisfy that requirement?
ans = 2×1
3 4
When you have a problem that is larger than your current abilities, break it into small, managable pieces. Solve each part, one at a time. Then put it all together. Eat a programming elephant one byte at a time.

その他の回答 (1 件)

Mathieu NOE
Mathieu NOE 2023 年 2 月 17 日
hello
here you are
S=[1,0,0;0,1,0;1,1,0;0,1,1;0,0,1]
S = 5×3
1 0 0 0 1 0 1 1 0 0 1 1 0 0 1
row_sum = sum(S,2);
rw_ind = find(row_sum>1)
rw_ind = 2×1
3 4

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by