2016-12-12 82 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