How to count "gaps" consisting of designated values in a vector?

3 ビュー (過去 30 日間)
Al_G
Al_G 2018 年 12 月 18 日
コメント済み: Matt J 2021 年 6 月 8 日
I have a very long vector of data that includes gaps of "invalid" values where data that's missing has been replaced with a designated missing data value (e.g. -1) or with "NaN".
invalid_values = [-1 NaN];
sample_data_vector = [22 23 22 24 -1 -1 -1 25 20 24 NaN Nan NaN 25 24 -1 -1 22 20 NaN 23];
I want to summarize info on the length of the gaps of invalid data (and no need to distinguish between whether the value is Nan or -1 or some other number). It might be helpful to know that my goal is to compare gap lengths over subsequent trials to determine whether gaps are getting longer/shorter and more/less frequent.
So for the above sample vector I'd get a matrix output that summarizes the following info about the gaps:
Length: 1 Number of occurences: 1
Length: 2 Number of occurences: 1
Length: 3 Number of occurences: 2
How would I write code to do this?

採用された回答

Matt J
Matt J 2018 年 12 月 18 日
編集済み: Matt J 2018 年 12 月 18 日
One way, assuming you have the Image Processing Toolbox,
bw=ismember(sample_data_vector, invalid_values)|isnan(sample_data_vector);
S=regionprops(bw,'Area');
gaplengths=[S.Area];
H = histcounts(gaplengths,1:max(gaplengths)+1) %output histogram
  5 件のコメント
Al_G
Al_G 2021 年 6 月 8 日
Revisiting this a few years later in an attempt to improve my code. Is there a straightforward way to obtain the indices of where the gaps start and end?
The ideal output would look like:
gap.length = [3, 3, 2, 1];
gap.start_indices = [5, 11, 16, 20];
gap.end_indices = [7, 13, 17, 20];
Matt J
Matt J 2021 年 6 月 8 日
That will just be,
gap.start_indices = find( diff([0,bw])>0 );
gap.end_indices = find( diff([bw,0])<0 );
gap.length = gap.end_indices-gap.start_indices+1;

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

タグ

製品


リリース

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by