2017-05-26 45 views
0

我有一個矩陣,數據的小樣本:在其他列的每個唯一的2個值查找列最低值Matlab的

A= 

1 3 658 

2 3 475 

5 3 769 

1 3 856 

6 7 1579 

2 3 678 

5 3 118 

6 7 617 

所以,現在,我要找到A列中的每一個獨特的組合B是C列中的最低值,最好是新的矩陣。

所以輸出將是:

B= 

1 3 658 

2 3 475 

5 3 118 

6 7 617 

你能指出我要做到這一點的最佳方式的方向? 在此先感謝

回答

3

sortrowsuniquerows選項的組合應該給你想要的結果。

A = sortrows(A); % After the sort unique combinations will be adjacent and with increasing values in 3rd column 
[~,ia] = unique(A(:,1:2),'rows'); % Find index of all the unique comb in col 1 & 2, unique only returns the first index 
B = A(ia,:); 
+0

偉大的作品,謝謝! – Rogier

1

如果前兩列中的值是正整數且在第三的值爲零,你也可以做到這一點,如下所示:

[ii, jj, vv] = find(accumarray(A(:,[1 2]), A(:,3), [], @min, 0, true)); 
B = [ii jj vv]; 
相關問題