根据序列前后的值替换NaN序列 [英] Replace NaN sequence according to values before and after the sequence
问题描述
如果有人可以帮助我解决这个问题,我将不胜感激...
I would appreciate if someone can help me with this problem...
我有一个向量
A = [NaN 1 1 1 1 NaN NaN NaN NaN NaN 2 2 2 NaN NaN NaN 2 NaN NaN 3 NaN NaN];
我想根据此逻辑填充NaN值.
I would like to fill the NaN values according to this logic.
1)如果NaN序列之前的值与序列后面的值不同=>将一半的NaN分配给第一个值,将一半的NaN分配给第二个值
1) if the value that precedes the sequence of NaN is different from the one that follows the sequence => assign half of the NaNs to the first value and half to the second value
2)如果NaN序列在2个相等的值之间=>用该值填充NaN.
2) if the NaN seqence is between 2 equal values => fill the NaN with that value.
A应为:
A = [1 1 1 1 1 1 1 (1) 2 2 2 2 2 2 2 2 2 2 3 3 3]
我把1设成小数点是因为我将该值分配给了前一半... NaN的序列是奇数.
I have put one 1 within brakets because I assigned that value to the first half...the sequence of NaNs is odd.
推荐答案
我在没有MATLAB的情况下在手机中键入了此信息-因此可能会出现一些问题.但这应该很接近:
I am typing this in my phone, without MATLAB - so there can be some issues. But this should be close:
t = 1:numel(A);
Anew = interp1(t(~isnan(A)),A(~isnan(A)),t,'nearest','extrap');
这篇关于根据序列前后的值替换NaN序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!