MATLAB Answers

0

How to find start and end points of non-zero elements in a vector

Asked by Eoin Horgan on 12 May 2015
Latest activity Commented on by Eoin Horgan on 12 May 2015
I have some vectors with mostly zeros and a few non-zero values all grouped together (see example plot). I want to know the index for the start and end points of these non-zero areas, i.e. the start and end of the peaks on the plot.
I can use find to get the non-zero areas, then iterate through with a loop to find everywhere the jump in values is greater than 1, but is there a better way to do this?

  0 Comments

Sign in to comment.

Tags

1 Answer

Answer by Walter Roberson
on 12 May 2015
 Accepted Answer

t = YourVec ~= 0;
findstr([0 t], [0 1])-1 %gives indices of beginning of groups
findstr([t 0], [1 0]) %gives indices of end of groups

  1 Comment

Thanks, much better than what i was doing.

Sign in to comment.