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