在矩阵中按列值分组查找值和最小值的索引 [英] Finding value and index of min value in a matrix, grouped by column values
本文介绍了在矩阵中按列值分组查找值和最小值的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Matlab在以下矩阵中找到每组数据点的最小值和索引:
I am trying to find the min value and index of each group of datapoints in the following matrix using matlab:
a=[0.3 1;
0.5 1;
0.2 1;
0.4 2 ;
0.43 2;
0.01 3;
0.3 3;
0.23 3];
数据按第2列中的值进行分组,即,前三行在第1组中,接下来的两行在第2组中,最后3行在第3组中.
The data is grouped by the value in column 2. i.e. the first three rows are in group 1, the next two rows are in group 2, the last 3 rows are in group 3.
谢谢
推荐答案
使用accumarray
和min
来查找每组的最小值:
Use accumarray
with min
to find the minimum values per group:
v = accumarray( a(:,2), a(:,1), [], @min )
要获取最小值的索引,请构建行:
To obtain the indices of the minima, construct the rows:
idx = find(ismember(a, [v, unique(a(:, 2))], 'rows'))
此外,如果您有m
个预定组,则可以使用(1:m)'
代替unique(...)
.
Also, if you have m
predetermined groups, you can use (1:m)'
instead of unique(...)
.
这篇关于在矩阵中按列值分组查找值和最小值的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文