Puzzler: Count unique nonzero periods in a timeseries without a for loop

3 ビュー (過去 30 日間)
Will Kinsman
Will Kinsman 2016 年 7 月 19 日
コメント済み: Will Kinsman 2016 年 7 月 19 日
given: a signal as a time series
return: discrete number of times is is holds a nonzero position
For example:
given: [0,1,0.5,0.7,0,1]
return: 2
given: [0,1,0,0,0,.3,1,0,0,1]
return: 3
given: [0,1]
return: 1
is there a way to do this task in a vector manner as opposed to a for loop that counts nonzero periods as it encounters them and flicks a counter.

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 7 月 19 日
編集済み: Azzi Abdelmalek 2016 年 7 月 19 日
a=[0,1,0,0,0,.3,1,0,0,1]
out=numel(strfind([0 logical(a)],[0 1]))
  1 件のコメント
Will Kinsman
Will Kinsman 2016 年 7 月 19 日
Thanks for the fast answer!! Nice simple solution.

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2016 年 7 月 19 日
Yes
[~, numRegions] = bwlabel(yourVector ~= 0);
  1 件のコメント
Will Kinsman
Will Kinsman 2016 年 7 月 19 日
sorry dont have imaging processing toolbox. but damn. nice solution.

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

カテゴリ

Help Center および File ExchangeStrategy & Logic についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by