interpolate NaNs only if less than 4 consecutive NaNs
古いコメントを表示
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 件のコメント
Oleg Komarov
2012 年 4 月 4 日
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
2012 年 4 月 4 日
Are you asking for help with the interpolation or identifying short and long sequences of nans?
Lindsey
2012 年 4 月 5 日
Jan
2012 年 4 月 5 日
Please insert additional information by editing the original question instead of adding a comment.
Lindsey
2012 年 4 月 12 日
Soni huu
2012 年 6 月 28 日
what about if the NaN is 2 space(" ") can you solve?
採用された回答
その他の回答 (1 件)
Geoff
2012 年 4 月 4 日
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 件のコメント
Jan
2012 年 4 月 12 日
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
2012 年 4 月 12 日
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. =)
カテゴリ
ヘルプ センター および File Exchange で Interpolation of 2-D Selections in 3-D Grids についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!