interpolate NaNs only if less than 4 consecutive NaNs
Mostrar comentarios más antiguos
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 comentarios
Oleg Komarov
el 4 de Abr. de 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
el 4 de Abr. de 2012
Are you asking for help with the interpolation or identifying short and long sequences of nans?
Lindsey
el 5 de Abr. de 2012
Jan
el 5 de Abr. de 2012
Please insert additional information by editing the original question instead of adding a comment.
Lindsey
el 12 de Abr. de 2012
Soni huu
el 28 de Jun. de 2012
what about if the NaN is 2 space(" ") can you solve?
Respuesta aceptada
Más respuestas (1)
Geoff
el 4 de Abr. de 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 comentarios
Jan
el 12 de Abr. de 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
el 12 de Abr. de 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. =)
Categorías
Más información sobre Interpolation of 2-D Selections in 3-D Grids en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!