【问题标题】:Numpy Permutations of a 3D Matrix3D 矩阵的 Numpy 排列
【发布时间】:2015-07-24 10:04:12
【问题描述】:

我有一个n x n x 2 矩阵,我想在不改变第三维元素顺序的情况下找到所有可能的排列。
例如,如果我的矩阵是 2 x 2 x 2 并且具有以下值:

[[[1,2], [3,4]],  
  [5,6], [7,8]]  

那么可能的排列是:

[[[1,2], [3,4]],  
  [7,8], [5,6]]   

[[[3,4], [1,2]],  
  [5,6], [7,8]]   

[[[1,2], [7,8]],  
  [5,6], [3,4]] 

等等。

换句话说,我想在查找排列时将元组视为单个值。

【问题讨论】:

    标签: python algorithm numpy matrix tuples


    【解决方案1】:

    怎么样:

    import numpy as np
    import itertools
    
    arr = np.array([[[1,2], [3,4]],
                    [[5,6], [7,8]]])
    
    for p in itertools.permutations(arr.reshape(-1, 2)):
        print(np.array(p).reshape(arr.shape))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-11
      • 2021-05-02
      • 2021-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-10
      相关资源
      最近更新 更多