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

时间:2017-06-17 14:16:59

标签: matlab matrix vector subset data-extraction

我正在制作exclude向量,以便删除包含user列表中矩阵exclude的第二列中存在的任何值的行。如何有效地执行此操作,而不使用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

1 个答案:

答案 0 :(得分:2)

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

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