Consider the following:
A = [1,1,1; 2,3,1; nan,3,1; nan,3,nan];
how would I replace the missing values with the previous values? For example:
data = 1 1 1 2 3 1 2 3 1 2 3 1
I can find the nan by isnan(data) but I don't know how to replace the values as described.
Amended:
I've managed to do this but not in a very concise way:
A = [1,1,1;2,3,1;nan,3,1;nan,3,nan];B = ~isnan(A);I = arrayfun(@(x)find(B(:,x),1,'last'),1:size(A,2));for i = 1:size(A,2); C = A(:,i); D = isnan(C); C(D) = C(I(i)); Final(:,i) = C;end
Could anyone suggest on a way to tidy this up?
Best Answer