I have a matrix with several NaN values and I want to replace those with the last non-NaN value. The matrix is very large so ideally I would do it in a vectorized fashion.
Here is a minimal working example:
M = [
NaN NaN NaN 3
7 NaN NaN 1
NaN NaN NaN 9
NaN 6 NaN NaN
NaN NaN NaN NaN
8 NaN NaN 8
NaN 5 NaN NaN
NaN NaN NaN NaN
9 NaN NaN NaN]
And the output should be:
Out = [
NaN NaN NaN 3
7 NaN NaN 1
7 NaN NaN 9
7 6 NaN 9
7 6 NaN 9
8 6 NaN 8
8 5 NaN 8
8 5 NaN 8
9 5 NaN 8]
I was wondering whether anyone faced a similar problem and knows an efficient way to solve it. I have read this similar post but all solutions seem to involve a for loop.
NaNs and not others? Is this per column? Per row? Per something else? - excaza