MATLAB: Selection of priority data

findfind functionnan

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

Best Answer

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