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

时间:2020-12-05 16:03:18

I am making an exclude vector, so that the rows containing any value present in the second column of the matrix user from the exclude list are removed. How do I do that efficiently, without using a for loop to iterate through user for each item in exclude one by one?

我正在创建一个排除向量,以便删除包含排除列表中矩阵用户的第二列中的任何值的行。如何有效地执行此操作,而不使用for循环来逐个遍历用户,逐个排除每个项目?

My code below does not work:

我的代码不起作用:

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

The last line gives the following error:

最后一行给出以下错误:

Error using ~=
Matrix dimensions must agree.

使用〜=矩阵尺寸时出错必须同意。

So, I am having to use this instead:

所以,我不得不使用它:

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

Example:

例:

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 ];

Desired output:

期望的输出:

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

1 个解决方案

#1


2  

Use 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 these row indices to get the row indices to be kept and use matrix indexing to keep these rows.

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

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

#1


2  

Use 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 these row indices to get the row indices to be kept and use matrix indexing to keep these rows.

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

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