MATLAB: Array manipulation

arrays

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

Best Answer

while true
K = find(isnan(PP),1,'first');
if isempty(K); break; end
PP(K) = PP(K-1);
end