I have an array that has about 70,000 rows that look like the following:
A = [ 1 2 3 4 5 6 0 723 1 22
1 2 3 4 5 7 0 NaN 2 10
etc.. ]
I would like to replace all NaN values in my array with the preceding value in the same column as the NaN value so that I would have something like this:
B = [ 1 2 3 4 5 6 0 723 1 22
1 2 3 4 5 7 0 723 2 10
etc.. ]
Since the array has about 70,000 rows, I am guessing some sort of loop would be the best way to achieve this. Using the following:
for ii = 1:size(A,2)
I = A(1,ii);
for jj = 2:size(A,1)
if isnan(A(jj,ii))
A(jj,ii) = I;
else
I = A(jj,ii);
end
end
end
I have been able to make a loop that replaces the whole row with the preceding row, but I am unsure how to modify it to target only NaN values. Any help would be greatly appreciated!