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

3 views (last 30 days)
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];

Answers (1)

dpb
dpb on 9 Jun 2021
Edited: dpb on 9 Jun 2021
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 :)

Tags

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!