interpolate NaNs only if less than 4 consecutive NaNs
Show older comments
Hello,
I have a vector of datapoints containing some NaNs. I'd like to interpolate the NaNs only if there are 3 or less consecutive NaNs. i.e. interpolate over short datagaps but not long ones.
Any ideas would be welcome. Thanks
6 Comments
Oleg Komarov
on 4 Apr 2012
Please post a minimum working example.
http://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers-and-get-a-fast-answer
Daniel Shub
on 4 Apr 2012
Are you asking for help with the interpolation or identifying short and long sequences of nans?
Lindsey
on 5 Apr 2012
Jan
on 5 Apr 2012
Please insert additional information by editing the original question instead of adding a comment.
Lindsey
on 12 Apr 2012
Soni huu
on 28 Jun 2012
what about if the NaN is 2 space(" ") can you solve?
Accepted Answer
More Answers (1)
Geoff
on 4 Apr 2012
Okay, here's a fun way to find the long sequences. You could interpolate the entire lot and then set the long sequences back to NaN. I'm using regexp because it's powerful =)
n = reshape(isnan(x), numel(x), 1); % ensure row-vector
[a, b] = regexp( char(n+'A'), 'B{4,}', 'start', 'end' );
This does string matching on sequences of 'B' (NaN) that are 4 characters or longer, and returns their start and end indices into the vectors a and b.
The nice thing about this is you can mess around with the regular expression to detect exactly what you want.
For example, to get only the indices of sequences with 3 or less NaNs, incorporating the non-NaN on either side, you would use:
'AB{1,3}A'
What you do with the indices is up to you.
2 Comments
Jan
on 12 Apr 2012
The reshaping of x can be simplified: "n = x(:)". Although I confuse this frequently, I think that this is a column vector, not a row vector.
The REGEXP method is nice. +1
It should be possible to use "conv(isnan(x), ones(1,4))" also.
Geoff
on 12 Apr 2012
Oh yeah, I didn't think of just doing:
n = isnan(x(:));
I thought at the time: "okay I want isnan(x)(:) but I can't do that!"
Duhhh. =)
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!