Array manipulation?
24 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi,
I'm looking for a piece of code that will replace NaN values in an array with the value immidiatly on top of the NaN value, I have paste an example below where I have replaced the values one by one, but that can hardly be the best way. would appreciate any assistance.
PPP =
2002
NaN
NaN
2008
2010
2012
NaN
2016
2018
2020
PP =
2002
2002
2002
2008
2010
2012
2012
2016
2018
2020
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 16 Apr. 2011
while true
K = find(isnan(PP),1,'first');
if isempty(K); break; end
PP(K) = PP(K-1);
end
0 Kommentare
Weitere Antworten (2)
Teja Muppirala
am 16 Apr. 2011
This should be simple enough. Obviously the first element can't be a nan though.
PP = PPP;
for n = 1:numel(PP)
if isnan(PP(n))
PP(n) = PP(n-1);
end
end
2 Kommentare
Walter Roberson
am 16 Apr. 2011
Why not start the "for" loop at 2 to prevent the possibility of indexing at 0 ?
Teja Muppirala
am 16 Apr. 2011
Yeah, that's a good point. My thinking was that you'd probably want to have an error there if you did in fact accidentally have a nan at the beginning.
Andrei Bobrov
am 16 Apr. 2011
fd1 = find(isnan(PPP))
PPP(fd1) = 0;
a = unique([fd1;fd1-1])
p = PPP(a);
PPP(a)=cell2mat(cellfun(@(x)cumsum(x),mat2cell(p,diff(find([p;1])),1),'UniformOutput', false))
0 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!