Filling the gaps in a vector
Ältere Kommentare anzeigen
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]
Akzeptierte Antwort
Weitere Antworten (3)
Image Analyst
am 23 Jan. 2013
0 Stimmen
Do you have the Image Processing Toolbox? If so, you can use imdilate, if you're clever about it.
1 Kommentar
joseph Frank
am 23 Jan. 2013
Azzi Abdelmalek
am 23 Jan. 2013
Bearbeitet: Azzi Abdelmalek
am 23 Jan. 2013
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 Kommentar
joseph Frank
am 24 Jan. 2013
Walter Roberson
am 23 Jan. 2013
0 Stimmen
You might also be interested in John D'Errico's FEX contribution inpaint_nans
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!