【问题标题】:Reorder matrix in Matlab by putting at the end rows with NaN在 Matlab 中通过将 NaN 放在末尾行重新排序矩阵
【发布时间】:2018-02-24 01:48:54
【问题描述】:

我在 Matlab 中有一个维度为 mx2 的矩阵。矩阵的某些行可以在第二列的第一个 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,:)

但它不起作用。

【问题讨论】:

    标签: matlab


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-11
      • 1970-01-01
      • 2012-06-09
      • 2012-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多