匹配和删除矩阵中的行

时间:2016-09-13 19:18:33

标签: matlab for-loop matrix rows

假设我有一个矩阵ssout(10x14)

1.15740740740741e-17    1.15740740740741e-18    1.50000000000000e-06    0.990000000000000   0.0900000000000000  3.45000000000000    1.10000000000000    1   2.04312609959835    1.41286525344677e-17    7.06432620699426e-18    0.0100000000000000  10000   0.00100000000000000
1.15740740740741e-17    1.15740740740741e-18    1.50000000000000e-06    0.990000000000000   0.0900000000000000  3.45000000000000    1.10000000000000    1   2.04246490864522    1.41286525344677e-17    7.06432620699426e-18    0.0100000000000000  10000   0.0100000000000000
1.15740740740741e-17    1.15740740740741e-18    1.50000000000000e-06    0.990000000000000   0.0900000000000000  3.45000000000000    1.10000000000000    1   2.04286618115584    1.41286525344677e-17    7.06432620699426e-18    0.0100000000000000  10000   0.100000000000000
1.15740740740741e-17    1.15740740740741e-18    1.50000000000000e-06    0.990000000000000   0.0900000000000000  3.45000000000000    1.10000000000000    1   2.04104656008947    1.41286525344677e-17    7.06432620699426e-18    0.0100000000000000  100000  0.00100000000000000
1.15740740740741e-17    1.15740740740741e-18    6.00000000000000e-06    0.990000000000000   0.0900000000000000  3.45000000000000    1.10000000000000    1   1.99946261970354    9.04233762205933e-16    4.52116877247632e-16    0.0100000000000000  100000  0.00100000000000000
1.15740740740741e-17    1.15740740740741e-18    1.25000000000000e-05    0.990000000000000   0.0900000000000000  3.45000000000000    1.10000000000000    1   1.86355987378850    8.17630355003923e-15    4.08815174015873e-15    0.0100000000000000  100000  0.00100000000000000
1.15740740740741e-17    1.15740740740741e-18    1.50000000000000e-06    0.990000000000000   0.0900000000000000  3.45000000000000    1.10000000000000    1   2.04723993665275    1.41286525344677e-17    7.06432620699426e-18    0.0100000000000000  100000  0.0100000000000000
1.15740740740741e-17    1.15740740740741e-18    6.00000000000000e-06    0.990000000000000   0.0900000000000000  3.45000000000000    1.10000000000000    1   1.99903967606786    9.04233762205933e-16    4.52116877247632e-16    0.0100000000000000  100000  0.0100000000000000
1.15740740740741e-17    1.15740740740741e-18    1.25000000000000e-05    0.990000000000000   0.0900000000000000  3.45000000000000    1.10000000000000    1   1.86368041811290    8.17630355003923e-15    4.08815174015873e-15    0.0100000000000000  100000  0.0100000000000000
1.15740740740741e-17    1.15740740740741e-18    1.50000000000000e-06    0.990000000000000   0.0900000000000000  3.45000000000000    1.10000000000000    1   2.04266773220586    1.41286525344677e-17    7.06432620699426e-18    0.0100000000000000  100000  0.100000000000000

我想通过消除没有其他类似行匹配的行来优化此矩阵。这里的标准是,如果这两行的列[1:2 4:8 12:14]相同,则两行匹配。对于行中的任何一行,如果根据我们的条件没有其他行匹配,我们将删除它。

我有这样的代码

ssout = sout;
rows = size(sout,1);
rowss = size(ssout,1);
c1 = 1:2
c2 = 4:8
c3 = 12:14
cout = 0

for i = 1: rows;
   for j = 1: rows-1;

       if isequal(sout(i,[c1 c2 c3]),ssout(j,[c1 c2 c3]));
           [i j]
           cout = cout +1 %increase cout each time it finds a match to row i
       end
       if cout < 2;%included comparison it to itself
           sout(i,:) = [];
           cout = 0;  %reset cout to 0     
           rows = size(sout,1); %update rows
       end

   end

end

我知道有些事情是错的,但不知道如何让它发挥作用。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

使用unique来确定结果中应包含哪些行而不是消除相似的行可能要简单得多。例如:

[~ , idx] = unique(ssout(:,[1:2 4:8 12:14]),'rows') % Find index of all unique rows
ssout_filtered = ssout(idx,:);