Filling the gaps in a vector
2 次查看(过去 30 天)
显示 更早的评论
I have a vector of nans
A=[nan;nan;2;nan;4;nan;nan;nan;7;nan;nan;nan;nan] how can I fill the nan gaps by the closest number (for beginning and mid values it is the closest upper;for the last values it is the closest lower value). i,e how can reproduce the vector to become A=[2;2;2;4;4;7;7;7;7;7;7;7;7]
0 个评论
采纳的回答
Andrei Bobrov
2013-1-23
编辑:Andrei Bobrov
2013-1-23
A=[nan;nan;2;nan;4;nan;nan;nan;7;nan;nan;nan;nan];
b = ~isnan(A);
k = cumsum(flipud(b));
k(k==0) = 1;
n = flipud(A(b));
s = n(k);
out = flipud(s);
or
t = ~isnan(A);
k = find(t) + 1;
z = zeros(size(A));
z(k(k <= numel(A))) = 1;
q = cumsum(z) + 1;
q(q > nnz(t)) = max(q) - 1;
p = A(t);
out = p(q);
0 个评论
更多回答(3 个)
Image Analyst
2013-1-23
Do you have the Image Processing Toolbox? If so, you can use imdilate, if you're clever about it.
Azzi Abdelmalek
2013-1-23
编辑:Azzi Abdelmalek
2013-1-23
A=[nan;nan;2;nan;4;nan;nan;nan;7;nan;nan;nan;nan]
B=A;
idx=find(isnan(A));
idx1=fliplr(find(~isnan(A)));
for k=1:numel(idx)
a=idx(k);
[~,ii]=min(abs(a-idx1));
B(idx(k))=A(idx1(ii));
end
Walter Roberson
2013-1-23
You might also be interested in John D'Errico's FEX contribution inpaint_nans
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!