Filling the gaps in a vector

조회 수: 1 (최근 30일)
joseph Frank
joseph Frank 2013년 1월 23일
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]

채택된 답변

Andrei Bobrov
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);

추가 답변 (3개)

Image Analyst
Image Analyst 2013년 1월 23일
Do you have the Image Processing Toolbox? If so, you can use imdilate, if you're clever about it.
  댓글 수: 1
joseph Frank
joseph Frank 2013년 1월 23일
Currently I don't but I will check with the university I think they have it

댓글을 달려면 로그인하십시오.


Azzi Abdelmalek
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
  댓글 수: 1
joseph Frank
joseph Frank 2013년 1월 24일
there is one additional 4

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2013년 1월 23일
You might also be interested in John D'Errico's FEX contribution inpaint_nans

카테고리

Help CenterFile Exchange에서 Geology에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by