How can I replace NaN elements with the nearest value in the same column?
9 次查看(过去 30 天)
显示 更早的评论
I am trying to replace NaN's in a vector field with the nearest value.
% I have:
M=
NaN 12
18 14
NaN NaN
NaN NaN
NaN 16
12 NaN
12 NaN
NaN 12
16 NaN
%I desire:
M=
18 12
18 14
12 16
12 16
12 16
12 12
12 12
16 12
16 12
Any information will be helpful. Thank you
2 个评论
AstroGuy1984
2017-4-25
编辑:AstroGuy1984
2017-4-25
What do you mean by "nearest"? Do you mean "next good value"? Because that's what you appear to desire. For example the second NaN in column 1 is closer to 18 than 12.
采纳的回答
Andrei Bobrov
2017-4-25
编辑:Andrei Bobrov
2017-4-26
FIXED 2
m = flipud(M)
t = ~isnan(m);
ii = cumsum(t);
ii(ii == 0) = 1;
ii = bsxfun(@plus,[0,ii(end,1:end-1)],ii);
m1 = m(t);
out = flipud(m1(ii))
6 个评论
Andrei Bobrov
2017-4-26
>> m = flipud(M);
t = ~isnan(m);
ii = cumsum(t);
ii(ii == 0) = 1;
ii = bsxfun(@plus,[0,ii(end,1:end-1)],ii);
m1 = m(t);
out = flipud(m1(ii))
out =
18 12
18 14
12 16
12 16
12 16
12 12
12 12
16 12
16 12
>>
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 NaNs 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!