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
댓글 수: 0
채택된 답변
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]
sum(S,2) % sum of the rows
sum(S,2) > 1 % testing if they exceed 1
find(sum(S,2) > 1) % which rows satisfy that requirement?
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.
댓글 수: 0
추가 답변 (1개)
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]
row_sum = sum(S,2);
rw_ind = find(row_sum>1)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!