Row numbers of the first zero, repeated

2 views (last 30 days)
Mat
Mat on 30 Oct 2014
Edited: Mat on 31 Oct 2014
I have a column vector of 7000 data points... of the pattern 0 0 0 0 30 60 90 120 119 118.... 2 1 0 0 0
I want to return the row number for the last cell before the number turns into a 0, every time this happens
Note: All the numbers are variable, and the sequences I want to extract varies in length.
To pick out the maximums I used [pks locs]=findpeaks(a(:,1)), want to do similar for just before the zeroes appear...

Accepted Answer

Image Analyst
Image Analyst on 30 Oct 2014
Your first element is a zero. Should that be included in the output? So you just want the last non-zero number in a stretch of non-zero numbers before it turns into a 0, right? Try this:
m = [0 0 0 0 30 60 90 120 119 118 2 1 0 0 0 3 4 0] % Create sample data.
dm = diff(m~=0) % Threshold and find element-to-element differences.
lastElements = find(dm == -1) % Find the indexes where it goes to zero.
  1 Comment
Mat
Mat on 31 Oct 2014
Edited: Mat on 31 Oct 2014
Thanks, works perfectly. No I wasn't interested in the first element.

Sign in to comment.

More Answers (0)

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!