How to count "gaps" consisting of designated values in a vector and obtain start and end indices?

9 ビュー (過去 30 日間)
Al_G
Al_G 2021 年 6 月 8 日
編集済み: dpb 2021 年 6 月 9 日
Adding on to a previous question I've asked:
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". I don't need to make a distinction between -1 or NaN. I want to count the length of the gaps, and capture the start and end indices.
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];
Is there a straightforward way to get an output that looks like the following:
data.gap.length = [3, 3, 2, 1];
data.gap.start_indices = [5, 11, 16, 20];
data.gap.end_indices = [7, 13, 17, 20];

回答 (1 件)

dpb
dpb 2021 年 6 月 9 日
編集済み: dpb 2021 年 6 月 9 日
v=sprintf('%d',ismember(sample_data_vector,invalid_values)|isnan(sample_data_vector));
iStart=strfind(v,'01')+1;
iEnd=strfind(v,'10');
N=iEnd-iStart+1;
I'll let you deal with the intermina.bly.long.and.convoluted.variable.names :)

カテゴリ

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

タグ

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by