the code
A = [
NaN 0.2500 NaN 0.3750 NaN
NaN 0.1100 NaN 0.4310 0.1250
NaN 0.1250 0.2500 0.3750 0.4310]
[M,N] = size(A)
[~,col1] = sort(~isnan(A),2,'descend')
row1 = repmat(1:M,N,1)'; %%//'
restructured_indices = sub2ind(size(A),row1(:),col1(:))
A = reshape(A(restructured_indices),M,N)
Output
A =
0.2500 0.3750 NaN NaN NaN
0.1100 0.4310 0.1250 NaN NaN
0.1250 0.2500 0.3750 0.4310 NaN
source
share