Clear Filters
Clear Filters

how find last row<>0

2 views (last 30 days)
piero
piero on 12 Aug 2023
Commented: piero on 12 Aug 2023
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 Comments
Dyuman Joshi
Dyuman Joshi on 12 Aug 2023
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 on 12 Aug 2023
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

Sign in to comment.

Accepted Answer

Matt J
Matt J on 12 Aug 2023
idx = find(~any(a,2),1,'last')-1

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!