interpolate NaNs only if less than 4 consecutive NaNs
Afficher commentaires plus anciens
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 commentaires
Oleg Komarov
le 4 Avr 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
le 4 Avr 2012
Are you asking for help with the interpolation or identifying short and long sequences of nans?
Lindsey
le 5 Avr 2012
Jan
le 5 Avr 2012
Please insert additional information by editing the original question instead of adding a comment.
Lindsey
le 12 Avr 2012
Soni huu
le 28 Juin 2012
what about if the NaN is 2 space(" ") can you solve?
Réponse acceptée
Plus de réponses (1)
Geoff
le 4 Avr 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 commentaires
Jan
le 12 Avr 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
le 12 Avr 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. =)
Catégories
En savoir plus sur Interpolation of 2-D Selections in 3-D Grids dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!