MATLAB: Last non nan observation

findnan

Hi,
I have a matrix and I want to find in each column the last non nan observation. Is there a neat way of doing it quickly? Regards

Best Answer

I'm not sure if you're looking for the indices (row number) or the actual value of the last non nan observation. So here are both:
B = ~isnan(A);
% indices
Indices = arrayfun(@(x) find(B(:, x), 1, 'last'), 1:size(A, 2));
% values
Values = arrayfun(@(x,y) A(x,y), Indices, 1:size(A, 2));