在矩阵中按列值分组查找值和最小值的索引

编程入门 行业动态 更新时间:2024-10-11 21:30:36
本文介绍了在矩阵中按列值分组查找值和最小值的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用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(...).

更多推荐

在矩阵中按列值分组查找值和最小值的索引

本文发布于:2023-10-28 01:25:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535088.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:矩阵   索引   最小值   中按列值

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!