MATLAB: Sort Matrix by Columns (descending by the number NaN entries)

matrixnansort

Once again I need your help!
I have a matrix with a variable number of columns. All these columns have a different number of NaN entries.
I would like to sort the matrix in a way that I have a new matrix with the first column having the lowest number of NaN entries,…
Thanks a lot in advance!

Best Answer

A=rand(10);ii=randperm(100);A(ii(1:40))=nan %example
[c,idx]=sort(sum(isnan(A)))
out=A(:,idx)