在Matlab中对矩阵进行重新排序,将最后一行放入NaN

时间:2018-02-23 17:10:38

标签: matlab

我在维度mx2的Matlab中有一个矩阵。矩阵的某些行可以在第二列中的第一个OR(不是和)中包含NaN。我想通过在末尾放置NaN的行来重新排序矩阵的行。此外,我想首先列出第二列中带有NaN的行,然后列出第一列中带有NaN的行。

例如

A=[NaN   11;
    10    NaN;
    5     8;
    2     0;
    NaN   3;
    9     NaN;
    1000  0];

A_new= [5 8; 
        2 0; 
        1000 0; 
        10 NaN; 
        9 NaN; 
        NaN 11; 
        NaN 3];

你能帮我写一下这段代码吗?我先尝试sort,然后重新排序

[ii ii] = sort(sum(isnan(A),2))
out = A(ii,:)

但它不起作用。

1 个答案:

答案 0 :(得分:1)

您可以使用isnan + sortrows

n = isnan(A);              % a binary matrix representing position of nan
[~,idx]= sortrows(n);      % get indexes for sorted elements
A_new = A(idx,:);          % reorder the matrix based on idx

A_new =
      5      8
      2      0
   1000      0
     10    NaN
      9    NaN
    NaN     11
    NaN      3
相关问题