python中numpy矩阵按照行排序,根据行逐步对Numpy Python矩阵进行排序

本文探讨了如何对numpy矩阵进行行排序,首先介绍了一个基于将矩阵转换为元组并应用Python内置排序的方法,然后提出使用np.lexsort函数,该方法在性能上更优,特别是对于大规模矩阵。通过示例展示了如何使用这两种方法对矩阵进行逐列排序。
摘要由CSDN通过智能技术生成

I have searched around and tried to find a solution to what seems to be a simple problem, but have come up with nothing. The problem is to sort a matrix based on its columns, progressively. So, if I have a numpy matrix like:

import numpy as np

X=np.matrix([[0,0,1,2],[0,0,1,1],[0,0,0,4],[0,0,0,3],[0,1,2,5]])

print(X)

[[0 0 1 2]

[0 0 1 1]

[0 0 0 4]

[0 0 0 3]

[0 1 2 5]]

I would like to sort it based on the first column, then the second, the third, and so on, to get a result like:

Xsorted=np.matrix([[0,0,0,3],[0,0,0,4],[0,0,1,1],[0,0,1,2],[0,1,2,5]])

print(Xsorted)

[[0,0,0,3]

[0,0,0,4]

[0,0,1,1]

[0,0,1,2]

[0,1,2,5]]

While I think it is possible to sort a matrix like this by naming the columns and all that, I would prefer to have a method for sorting th

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值