Matlab:当列中有重复值时删除行

时间:2013-07-11 18:32:14

标签: performance matlab

我在列相同时删除行时遇到问题。

我使用了for和if循环,但运行时间太长。

我在想是否有更高效,更快的运行时方法。 说

A=[ 2 4 6 8; 3 9 7 9; 4 8 7 6; 8 5 4 6; 2 10 11 2]

我希望结果是

A=[ 2 4 6 8;
    4 8 7 6;
    8 5 4 6]

由于重复的'9'而消除了第2行,并且由于重复的'2'而删除了第5行。

2 个答案:

答案 0 :(得分:1)

您可以使用sortdiff来识别具有重复值的行

A = A(all(diff(sort(A'))),:)

返回

A =
     2     4     6     8
     4     8     7     6
     8     5     4     6

答案 1 :(得分:0)

这里的技巧是如何以有效的方式找到具有重复值的行 怎么样:

% compare all-vs-all for each row using `bsxfun`
>> c = bsxfun( @eq, A, permute( A, [1 3 2] ) );
>> c = sum( c, 3 ); % count the number of matches each element has in the row
>> c = any( c > 1, 2 ); % indicates rows with repeated values - an element with more than one match
>> A = A( ~c, : )
 A =
  2     4     6     8
  4     8     7     6
  8     5     4     6