【问题标题】:Find lowest value in column for every unique 2 values in other columns Matlab在其他列 Matlab 中为每个唯一的 2 个值查找列中的最小值
【发布时间】:2017-05-26 02:02:16
【问题描述】:
我有一个矩阵,数据的小样本:
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
您能告诉我最好的方法吗?
提前致谢
【问题讨论】:
标签:
matlab
matrix
combinations
unique-values
【解决方案1】:
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,:);
【解决方案2】:
如果前两列的值为正整数,第三列的值为非零,也可以这样:
[ii, jj, vv] = find(accumarray(A(:,[1 2]), A(:,3), [], @min, 0, true));
B = [ii jj vv];