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

9 vues (au cours des 30 derniers jours)
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];

Réponses (1)

dpb
dpb le 9 Juin 2021
Modifié(e) : dpb le 9 Juin 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 :)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by