【问题标题】:Numpy: sort Matrix's elements by index [closed]Numpy:按索引对矩阵元素进行排序[关闭]
【发布时间】:2019-05-13 14:40:15
【问题描述】:

我需要对一个由数组定义的矩阵进行排序

example = np.array([[00, 01, 02, 03, 04],
               [10, 11, 12, 13, 14],
               [20, 21, 22, 23, 24],
               [30, 31, 32, 33, 34],
               [40, 41, 42, 43, 44]])

通过索引列表,如index = [1, 3],并获得:

example = np.array([[11, 13, 10, 12, 14],
               [31, 33, 30, 32, 34],
               [01, 03, 00, 02, 04],
               [21, 23, 30, 22, 24],
               [41, 43, 40, 42, 44]])

其中第一个左上角 2x2 子矩阵由位置 [1,1]、[1,3]、[3,1] 和 [3,3] 中的元素组成。

我使用了 for-cycle 和 numpy.extract(我认为这不正确),但我确信存在最简单的方法。

【问题讨论】:

  • 您是否尝试过编写代码来解决您的问题?如果是这样,请发布它。 StackOverflow 不是代码编写服务。

标签: css arrays numpy matrix indexing


【解决方案1】:

permutation 中创建你想要的排列,然后使用:

e = example[:, permutation][permutation,:]

例如你的情况:

permutation = [1, 3, 0, 2, 4]

【讨论】:

    猜你喜欢
    • 2021-06-29
    • 2017-05-24
    • 2018-04-29
    • 1970-01-01
    • 2012-04-22
    • 2021-03-08
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    相关资源
    最近更新 更多