朱莉娅:按第2列然后第3列对矩阵进行排序 [英] Julia: Sort Matrix by column 2 then 3

查看:65
本文介绍了朱莉娅:按第2列然后第3列对矩阵进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按第2列然后按3列对矩阵A进行排序.

I would like to sort my matrix A by column 2 then 3.

A = round(randn(100,4))

也许像这样:

sort(A,(0,2:3))
100x4 Array{Float64,2}:
  0.0  -2.0  -2.0  -1.0
 -1.0  -2.0  -1.0   1.0
  1.0  -2.0  -1.0   2.0
 -1.0  -2.0   0.0   0.0
 -1.0  -2.0   0.0  -1.0
 -0.0  -2.0   0.0  -1.0
  1.0  -2.0   0.0   0.0
  1.0  -2.0   1.0  -1.0
 -0.0  -2.0   2.0  -1.0
 -0.0  -1.0  -2.0   1.0
  ⋮                    
 -0.0   1.0   0.0   1.0
  1.0   1.0   1.0   1.0
  0.0   1.0   1.0  -1.0
 -0.0   1.0   2.0   0.0
 -0.0   2.0  -1.0   0.0
 -2.0   2.0  -1.0   1.0
  2.0   2.0  -0.0  -1.0
 -1.0   2.0  -0.0  -1.0
  1.0   2.0   0.0   2.0
 -1.0   2.0   2.0   0.0

推荐答案

有一个sortrows函数带有一个by关键字,可让您执行此操作:

There is a sortrows function that takes a by keyword that lets you do this:

julia> sortrows(A, by=x->(x[2],x[3]))
100x4 Array{Float64,2}:
  2.0  -3.0  -0.0   0.0
 -1.0  -2.0  -1.0  -1.0
 -0.0  -2.0  -0.0   0.0
  0.0  -2.0   0.0  -1.0
  1.0  -2.0   1.0   2.0
 -0.0  -2.0   1.0  -1.0
 -1.0  -1.0  -2.0   1.0
 -1.0  -1.0  -2.0  -0.0
 -1.0  -1.0  -1.0   1.0
 -0.0  -1.0  -1.0   0.0
  ⋮
 -0.0   1.0   1.0  -1.0
 -0.0   1.0   2.0   1.0
  0.0   1.0   2.0   0.0
 -1.0   2.0  -2.0   1.0
  0.0   2.0  -2.0  -2.0
  1.0   2.0  -1.0   0.0
  0.0   2.0  -1.0  -0.0
 -1.0   2.0   0.0  -1.0
 -0.0   2.0   2.0   0.0
  1.0   3.0   2.0   1.0

排序API非常灵活-您可以在此处找到文档.

The sorting API is pretty flexible – you can find documentation here.

这篇关于朱莉娅:按第2列然后第3列对矩阵进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆