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