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

9 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by