2016-12-12 85 views
0

我想在没有循环的情况下在Octave上合并一个表。下面是我想要做的一个例子:Octave - 按列合并矩阵无循环

column1 column2 column3 
1  1  1 
1  2  2 
1  1  2 
1  1  3 
2  1  3 
2  1  1 

喜欢这样的:我试图用循环做

column1 column2 column3 
1  1  6 
1  2  2 
2  1  4 

但它确实太慢了。有没有循环可以做到这一点的功能?

+1

我不能在这里看到的系统和你真的应该描述你希望如何“合并“他们。并用for循环显示你的代码,它可以做你想做的事 – Andy

回答

4

您可以使用uniqueaccumarray组合:

A = [1  1  1 
    1  2  2 
    1  1  2 
    1  1  3 
    2  1  3 
    2  1  1]; 


[U,~,ind] = unique(A(:,1:2),'rows'); %get the unique rows based on the column 1 and 2. 
AC  = accumarray(ind,A(:,3)); %sum the value of column 3 based on column 1 and 2 
M   = [U,AC] % creation of the final matrix. 

结果:

M = 

    1 1 6 
    1 2 2 
    2 1 4 
+0

非常感谢!这正是我想要做的。谢谢。 – Joseph