0

我有一个矩阵,一个小数据样本:

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

你能给我指出最好的方法吗?提前致谢

4

2 回答 2

3

sortrows和选项unique的组合rows应该会给你想要的结果。

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,:); 
于 2017-05-26T02:18:27.737 回答
1

如果前两列的值为正整数,而第三列的值为非零,您也可以这样做:

[ii, jj, vv] = find(accumarray(A(:,[1 2]), A(:,3), [], @min, 0, true));
B = [ii jj vv];
于 2017-05-26T08:25:36.467 回答