如何在 Mathematica 中按第二列对矩阵进行排序?

发布于 2024-12-18 01:00:24 字数 256 浏览 4 评论 0原文

想象一下您有:

a = {{5, 1, 1}, {2, 0, 7}, {3, -4, 6}}

并且您想按第二列对其进行排序,以获取

b = {{3, -4, 6}, {2, 0, 7}, {5, 1, 1}}

我已尝试使用 SortBy[a, Last] 并适用于最后一列,但我无法让它工作对于第二列。

提前致谢 :-)

Imagine you have:

a = {{5, 1, 1}, {2, 0, 7}, {3, -4, 6}}

and you want to order it by the second column, to get

b = {{3, -4, 6}, {2, 0, 7}, {5, 1, 1}}

I have tried with SortBy[a, Last] and works for the last column, but I can't get it to work for the second column.

Thanks in advance :-)

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

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

发布评论

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

评论(6

余厌 2024-12-25 01:00:24

这确实有效:

SortBy[a,#[[2]]&]

This does work:

SortBy[a,#[[2]]&]
撩心不撩汉 2024-12-25 01:00:24

或者,

a[[Ordering[a[[All, 2]]]]]

Alternatively,

a[[Ordering[a[[All, 2]]]]]
摇划花蜜的午后 2024-12-25 01:00:24

在这里,对于强制性计时(我将基本的 Sort 添加到方法中):

a = RandomReal[{0, 10}, {1000000, 3}];

Sort[a, #2[[2]] < #1[[2]] &]; // Timing

(* ==> {34.367, Null} *)

SortBy[a, #[[2]] &]; // Timing

(* ==> {0.436, Null} *)

 a[[Ordering[a[[All, 2]]]]]; // Timing

(* ==> {0.234, Null}, Chris wins *)

And here, for the obligatory timing (I added the basic Sort to the methods):

a = RandomReal[{0, 10}, {1000000, 3}];

Sort[a, #2[[2]] < #1[[2]] &]; // Timing

(* ==> {34.367, Null} *)

SortBy[a, #[[2]] &]; // Timing

(* ==> {0.436, Null} *)

 a[[Ordering[a[[All, 2]]]]]; // Timing

(* ==> {0.234, Null}, Chris wins *)
活泼老夫 2024-12-25 01:00:24

也许你可以使用这个网址: http://12000.org/my_notes/mma_matlab_control/KERNEL/ node99.htm

您可以使用的代码:

a={{300,48,2},{500,23,5},{120,55,7},{40,32,1}};
b=SortBy[a, #[[2]]&]

结果:

Out[9]= {{500,23,5},{40,32,1},{300,48,2},{120,55,7}}

Maybe you can use this url: http://12000.org/my_notes/mma_matlab_control/KERNEL/node99.htm

Code you can use:

a={{300,48,2},{500,23,5},{120,55,7},{40,32,1}};
b=SortBy[a, #[[2]]&]

Result:

Out[9]= {{500,23,5},{40,32,1},{300,48,2},{120,55,7}}
我乃一代侩神 2024-12-25 01:00:24

如果您的数据是:

a = {{5, 1, 1}, {2, 1, 7}, {3, -4, 6}}

并且您需要对第二个元素进行稳定排序,则会产生:

{{3, -4, 6}, {5, 1, 1}, {2, 1, 7}}

尝试使用 SortBy 解决此问题可能会非常令人沮丧,除非您意识到这一点:

SortBy[a, {#[[2]] &}]

{ } 括号很重要。

If your data were:

a = {{5, 1, 1}, {2, 1, 7}, {3, -4, 6}}

And you needed a stable sort on the second element, yielding:

{{3, -4, 6}, {5, 1, 1}, {2, 1, 7}}

It could be very frustrating to try to solve this with SortBy, unless you were aware of this:

SortBy[a, {#[[2]] &}]

The {} brackets are important.

温柔女人霸气范 2024-12-25 01:00:24

只是在这方面的一个提示:
当使用像 Sqrt[...] 这样的非原子对象时,你可能会得到意想不到的结果:

SortBy[Range[10], -Sqrt[#] &]
{9, 4, 1, 8, 2, 3, 5, 6, 7, 10}

这是由于

排序通常通过将较短的表达式放在前面来对表达式进行排序,并且
然后以深度优先的方式比较零件。 (Mathematica 参考手册)。

当需要数字排序时,始终使用 N。

just a tip in this context:
when using non-atomar objects like Sqrt[...] you might get unexpected results:

SortBy[Range[10], -Sqrt[#] &]
{9, 4, 1, 8, 2, 3, 5, 6, 7, 10}

this is due to

Sort usually orders expressions by putting shorter ones first, and
then comparing parts in a depth-first manner. (Mathematica reference manual).

Always use N, when a numeric sort is desired.

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