I am looking for a way to remove the NaN numbers from a matrix in MATLAB efficiently (i.e. without using a for loop)
I will provide a quick example to illustrate what I am trying to achieve:
Say I have a matrix M:
3.00 1.00
1.00 3.00
NaN NaN
3.00 3.00
1.00 1.00
NaN NaN
NaN NaN
NaN NaN
NaN NaN
NaN NaN
I would like to find a way to change this to
3.00 1.00
1.00 3.00
3.00 3.00
1.00 1.00
I am currently trying to do this via M(isfinite(M)) but that ends up returning a vector instead of the matrix. Is there a trick to have it return a matrix instead?
If you have either no NaNs or all NaNs on each row, you can do the removal using:
M(isfinite(M(:, 1)), :)