【问题标题】:How to remove all the rows from a matrix that match values in another vector?如何从矩阵中删除与另一个向量中的值匹配的所有行?
【发布时间】:2017-06-17 14:16:58
【问题描述】:

我正在制作一个exclude 向量,以便从exclude 列表中删除包含矩阵user 第二列中存在的任何值的行。我如何有效地做到这一点,而不使用for 循环来逐一迭代user 中的每个项目exclude

我下面的代码不起作用:

count=0;

% Just showing how I am constructing `exclude`, to show that it can be long.
% So, manually removing each item from `exclude` is not an option.
% And using a for loop to iterate through each element in `exclude` can be inefficient.
for b=1:size(user_cat,1)
    if user_cat(b,4)==0
        count=count+1;
        exclude(count,1) = user_cat(b,1);
    end
end

% This is the important line of focus. You can ignore the previous parts.
user = user(user(:,2)~=exclude(:),:);

最后一行给出以下错误:

使用~= 时出错
矩阵尺寸必须一致。

所以,我不得不改用这个:

for b=1:size(exclude,1)
    user = user(user(:,2)~=exclude(b,1),:);
end

示例:

user=[1433100000.00000  26    620260    7   1433100000000.00    0                   0                 2  1  100880     290   23
      1433100000.00000  26    620260    7   1433100000000.00    0                   0                 2  1  100880     290   23
      1433100000.00000  25    620160    7   1433100000000.00    0                   0                 2  1  100880    7274   22
      1433100000.00000  21    619910    7   1433100000000.00    24.1190000000000    120.670000000000  2  0  100880   53871   21
      1433100000.00000  19    620040    7   1433100000000.00    24.1190000000000    120.670000000000  2  0  100880   22466   21
      1433100000.00000  28    619030    7   1433100000000.00    24.6200000000000    120.810000000000  2  0  100880  179960   16
      1433100000.00000  28    619630    7   1433100000000.00    24.6200000000000    120.810000000000  2  0  100880   88510   16
      1433100000.00000  28    619790    7   1433100000000.00    24.6200000000000    120.810000000000  2  0  100880   12696   16
      1433100000.00000   7  36582000    7   1433100000000.00    0                   0                 2  0  100880   33677   14
      1433000000.00000  24    620010    7   1433000000000.00    0                   0                 2  1  100880    3465   14
      1433000000.00000   4  36581000    7   1433000000000.00    0                   0                 2  0  100880   27809   12
      1433000000.00000  20    619960    7   1433000000000.00    0                   0                 2  1  100880     860   11
      1433000000.00000  30    619760    7   1433000000000.00    25.0060000000000    121.510000000000  2  0  100880   34706   10
      1433000000.00000  33    619910    7   1433000000000.00    0                   0                 2  0  100880   15060    9
      1433000000.00000  26    619740    6   1433000000000.00    0                   0                 2  0  100880   52514    8
      1433000000.00000  18    619900    6   1433000000000.00    0                   0                 2  0  100880   21696    8
      1433000000.00000  16    619850    6   1433000000000.00    24.9910000000000    121.470000000000  2  0  100880   10505    1
      1433000000.00000  16    619880    6   1433000000000.00    24.9910000000000    121.470000000000  2  0  100880    1153    1
      1433000000.00000  28    619120    6   1433000000000.00    0                   0                 2  0  100880  103980   24
      1433000000.00000  21    619870    6   1433000000000.00    0                   0                 2  0  100880    1442   24];

exclude=[ 3
          4
          7
         10
         17
         18
         19
         28
         30
         33 ];

期望的输出:

1433100000.00000  26    620260    7   1433100000000.00    0                   0                 2  1  100880     290   23
1433100000.00000  26    620260    7   1433100000000.00    0                   0                 2  1  100880     290   23
1433100000.00000  25    620160    7   1433100000000.00    0                   0                 2  1  100880    7274   22
1433100000.00000  21    619910    7   1433100000000.00    24.1190000000000    120.670000000000  2  0  100880   53871   21
1433000000.00000  24    620010    7   1433000000000.00    0                   0                 2  1  100880    3465   14
1433000000.00000  20    619960    7   1433000000000.00    0                   0                 2  1  100880     860   11
1433000000.00000  26    619740    6   1433000000000.00    0                   0                 2  0  100880   52514    8
1433000000.00000  16    619850    6   1433000000000.00    24.9910000000000    121.470000000000  2  0  100880   10505    1
1433000000.00000  16    619880    6   1433000000000.00    24.9910000000000    121.470000000000  2  0  100880    1153    1
1433000000.00000  21    619870    6   1433000000000.00    0                   0                 2  0  100880    1442   24

【问题讨论】:

    标签: matlab matrix vector subset data-extraction


    【解决方案1】:

    使用ismember 查找user 的第二列的索引,其中存在exclude 的元素以获取要删除的行的索引。 Negate这些行索引来获取要保留的行索引并使用矩阵索引来保留这些行。

    user = user(~ismember(user(:,2),exclude),:);
    

    【讨论】:

    • 啊,我误解了这个问题。我以为他只想在完美匹配的情况下删除一行。
    • @LeanderMoesinger 抱歉,没有意识到这个问题可能会被误解。编辑问题以包括示例输入和输出。此答案不会产生所需的输出。
    • @Kristada673 它给出的输出与您在问题中发布的完全相同
    • @SardarUsama 嗯,它确实在这个样本输入上给出了正确的输出。但在我的实际数据中,有更多列,它没有。可能是因为存在额外的列吗?
    • @Kristada673 提供一个不起作用的例子
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 1970-01-01
    • 2019-12-10
    相关资源
    最近更新 更多