MATLAB 中的 GROUP BY

发布于 2024-12-28 06:37:13 字数 251 浏览 2 评论 0原文

我想做 SQL 的 GROUP BY 在 MATLAB 中所做的事情。例如,

M = [
1, 5;
2、5;
3, 5;
1, 6;
2, 6;
1,7 ]

SQL:从 M(c1, c2) GROUP BY 2 中选择 MAX(c1), c2

结果 = [
3, 5;
2, 6;
1, 7]

我怎样才能在 Matlab 中做到这一点?

I want to do what SQL's GROUP BY does in MATLAB. For example,

M = [
1, 5;
2, 5;
3, 5;
1, 6;
2, 6;
1,7 ]

SQL: SELECT MAX(c1), c2 FROM M(c1, c2) GROUP BY 2

Result = [
3, 5;
2, 6;
1, 7]

How can I do this in Matlab?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青春如此纠结 2025-01-04 06:37:13

统计工具箱中的grpstats可以做到这一点:

>> [grpstats(M(:,1), M(:,2), {'max'}) unique(M(:,2))]

ans =

     3     5
     2     6
     1     7

grpstats in the Statistics Toolbox can do this:

>> [grpstats(M(:,1), M(:,2), {'max'}) unique(M(:,2))]

ans =

     3     5
     2     6
     1     7
锦爱 2025-01-04 06:37:13

如果您不介意进行一些预处理来获取顺序(或者如果第一列很好地从 1 到 n 构建),您可以这样做:

accumarray([1 2 3 1]',[11 12 13 14]',[],@max)

这将给出:

14
12
13

或者在您的情况下:

accumarray(M(:,1),M(:,2),[],@max)

注意顺序。例如,第二个数字将对应于 M(:,1) == 2

If you don't mind doing some preprocessing to get the order (or if the first column is nicely built from 1 to n), you can do it like this:

accumarray([1 2 3 1]',[11 12 13 14]',[],@max)

This will give:

14
12
13

Or in your case:

accumarray(M(:,1),M(:,2),[],@max)

Note the order. The second number for example, will correspond to M(:,1) == 2

作死小能手 2025-01-04 06:37:13

我认为有一个简单的解决方案。
这是我在 Matlab 上测试的结果,它有效:

>> M = [
1, 5;
2, 5; 
3, 5;
1, 6;
2, 6;
1,7 ];

>> grpstats(M,M(:,2),{'max'})

ans =

     3     5
     2     6
     1     7

I think there is a simple solution to this.
Here is what I tested on Matlab and it worked:

>> M = [
1, 5;
2, 5; 
3, 5;
1, 6;
2, 6;
1,7 ];

>> grpstats(M,M(:,2),{'max'})

ans =

     3     5
     2     6
     1     7
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文