如何从矩阵中删除与另一个向量中的值匹配的所有行?

How to remove all the rows from a matrix that match values in another vector?

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

我的以下代码不起作用:

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(:),:);

最后一行报错如下:

Error using ~=
Matrix dimensions must agree.

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

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

使用ismember to find the indices of the second column of user where elements of exclude exist to get the indices of the rows to be removed. Negate这些行索引来获取要保留的行索引,并使用矩阵索引来保留这些行。

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