how find last row<>0

조회 수: 2 (최근 30일)
piero
piero 2023년 8월 12일
댓글: piero 2023년 8월 12일
a=[3 4 5 0 3 4 0;4 5 0 0 0 0 0;0 0 0 0 0 0 0]
a =
3 4 5 0 3 4 0
4 5 0 0 0 0 0
0 0 0 0 0 0 0
i want to know the last rows wich element are ==0 ..solution idx=2!
i try this solution :find(sum(a,2)==0)-1 but it's no good because example :the sum 3 3 0 -3 -3 0 =0 but is not correct
  댓글 수: 2
Dyuman Joshi
Dyuman Joshi 2023년 8월 12일
Your phrasing is not clear (atleast to me).
a=[3 4 5 0 3 4 0;4 5 0 0 0 0 0;0 0 0 0 0 0 0]
a = 3×7
3 4 5 0 3 4 0 4 5 0 0 0 0 0 0 0 0 0 0 0 0
As you said, the output for the above array is 2, what is the logic behind that?
piero
piero 2023년 8월 12일
a=flip(a)
for i=1:height(a)
if sum(a(i,:),2)>0 and sum(a(i+1,:),2)==0
memRow=i;
break;
end
end
t's similar this code but i don't to use loop and sum
I want to store the idx of the last row where there is at least one number <>0

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2023년 8월 12일
idx = find(~any(a,2),1,'last')-1

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by