Hello,
I'm trying to solve a problem. I have a vector of data such as:
data=[3;4;8;NaN;NaN;NaN;7;4;3;NaN;NaN;2;3;NaN;9]
length of data can be variable and NaN value phases can happen everywhere within a vector.
I need to find the first NaN value and count the number of next NaN. However, I do not want to take into account any other NaN value after the first portion of NaN.
In this case my result would be: First NaN at raw 4. Three NaN values. I know how to use the function find, indeed this is the code I used as far:
findNaN = find(isnan(data));
The problem is that in that way I will get the others NaN too that I do not want to locate.
Any idea?
Thanks, Giuseppe

 채택된 답변

Shoaibur Rahman
Shoaibur Rahman 2014년 12월 27일

1 개 추천

data=[3;4;8;NaN;NaN;NaN;7;4;3;NaN;NaN;2;3;NaN;9];
findNaN = find(isnan(data));
first = findNaN(1);
numberNaN = 1;
for k = first+1:length(data)
if isnan(data(k))
numberNaN = numberNaN+1;
else break
end
end
first, numberNaN

추가 답변 (2개)

Image Analyst
Image Analyst 2014년 12월 27일

1 개 추천

If you're lucky enough to own the Image Processing Toolbox, it's easy. Just find the nan's, label then, and count the number in the first group. Here is the code:
labeledVector = bwlabel(isnan(data)) % Find nan's and label.
numberOfNansInFirstGroup = sum(labeledVector == 1) % Count the # of elements in group #1
Giuseppe
Giuseppe 2014년 12월 27일

1 개 추천

thanks for helping to improve my code

카테고리

도움말 센터File Exchange에서 NaNs에 대해 자세히 알아보기

질문:

2014년 12월 26일

답변:

2014년 12월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by