【问题标题】:How to find the original index after removing rows?删除行后如何找到原始索引?
【发布时间】:2017-05-22 18:38:37
【问题描述】:

我有一个大小为 15000*1000 的矩阵 A。我从中删除了 1000 个全零行,并得到一个大小为 14000*1000 的新矩阵 B。假设我从新矩阵 B 中选择第 10000 行。如何在原始矩阵 A 中找到该行的原始索引?换句话说,我想根据我的数据中某行的新索引找到原始索引。例如,如果原矩阵 A 中的第 14999 行变成新矩阵 B 中的第 14000 行,那么在 B 中只给出 14000 的情况下,如何推导出 14999?谢谢!

【问题讨论】:

    标签: matlab matrix


    【解决方案1】:

    我会后退一步,使用用于从原始矩阵中删除(或选择)行的索引向量。假设您有这样的操作:

    A = randn(150,100); % Sample A matrix
    rejectIdx = randi(150,10,1); % 10 rows which should be removed (selected at random here)
    B = A; B(rejectIdx,:) = []; % Remove the ten rows from A
    

    创建行索引向量并使用相同的rejectIdx 变量删除行索引

    origIdx = 1:150; origIdx(rejectIdx) = []; % Save row indexes and remove index using the same idx
    

    现在要查找 A 中的哪一行对应于 B 中的第 i 行,可以简单地执行

    A(origIdx(i),:)
    

    【讨论】:

      猜你喜欢
      • 2013-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-06
      相关资源
      最近更新 更多