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

25 ビュー (過去 30 日間)
Eoin Horgan
Eoin Horgan 2015 年 5 月 12 日
コメント済み: Walter Roberson 2020 年 7 月 17 日
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?

採用された回答

Walter Roberson
Walter Roberson 2015 年 5 月 12 日
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
  3 件のコメント
Amrit Zoad
Amrit Zoad 2020 年 7 月 17 日
編集済み: Amrit Zoad 2020 年 7 月 17 日
Thanks Walter! I am in a similar situation but I want to find the center value of the non-zero group of points.
Walter Roberson
Walter Roberson 2020 年 7 月 17 日
mask = logical(YourVector(:).'); %(:).' to force row vector
starts = strfind([false, mask], [0 1]);
stops = strfind([mask, false], [1 0]);
centers = mean([starts;stops]);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAuthor Block Masks についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by