使用置换向量对矩阵重新排序,但保持矩阵的原始大小

时间:2020-06-04 08:04:45

标签: python arrays numpy permutation reorderlist

我有一个简单的问题,但无法解决。我有一个C_temp 16x16矩阵(size = 16),是从另一个矩阵复制来的。

C_temp = np.zeros((size, size))                                                                                
C_temp = np.copy(C_in)

然后,我有一个置换列表(或numpy数组,我不知道这是否重要):

print('index_reorder =', index_reorder)给出:

index_reorder = ', array([2, 4, 0, 5, 1, 3, 7, 8]))

我想进行index_reorder沿x axisy axis指示的排列。

      C_temp = np.copy(C_in)
      C_temp = C_temp[:, index_reorder]
      C_temp = C_temp[index_reorder, :]
      C_new = np.copy(C_temp)

但不幸的是,新矩阵C_new的大小减小到8x8。

这不是我想要得到的:我想为C_new矩阵(16x16)保持相同的大小,即在保持置换矩阵C_temp的整个大小的同时进行置换。

如何执行此全局置换?

我相信这被称为“就地置换”,不是吗?

更新1:这是C_in矩阵16x16的示例

C_in = ', array([[ 5.39607129e+06,  1.79979372e+06, -2.46370980e+06,
        -1.12590397e+06,  2.54997996e+03, -3.48237530e+02,
         1.77139942e+05,  2.10555125e+04, -2.24912032e+05,
        -9.79292472e+01, -1.63415352e+05, -8.65388775e+01,
        -8.10556705e+04, -6.40511456e+01,  1.31499502e+04,
        -4.80973452e+01],
       [ 1.79979372e+06,  1.85207497e+07, -5.97280544e+06,
        -4.86527342e+05, -9.46833729e+05, -2.10321296e+05,
        -7.71198259e+05, -8.88750203e+04, -1.66150873e+06,
        -3.20782728e+02, -1.45257426e+06, -2.86060423e+02,
        -1.10641471e+06, -2.17539743e+02, -9.34181143e+05,
        -1.77667406e+02],
       [-2.46370980e+06, -5.97280544e+06,  3.36326384e+06,
         5.88733451e+05,  3.35606646e+05,  8.96417015e+04,
         1.12240864e+05,  1.35483472e+04,  6.10023925e+05,
         1.26679014e+02,  5.65166386e+05,  1.21455772e+02,
         4.43234727e+05,  9.80424886e+01,  3.68206009e+05,
         8.44106515e+01],
       [-1.12590397e+06, -4.86527342e+05,  5.88733451e+05,
         3.34731505e+05, -3.26665859e+04, -7.14038524e+03,
        -7.25370986e+04, -8.44842826e+03,  4.40874561e+04,
         2.82933253e+01,  2.77238713e+04,  2.47986977e+01,
         7.27381187e+03,  1.80784440e+01, -1.87787106e+04,
         1.31142301e+01],
       [ 2.54997996e+03, -9.46833729e+05,  3.35606646e+05,
        -3.26665859e+04,  7.90884228e+04,  1.92364617e+04,
         5.66130910e+04,  6.70772964e+03,  1.07063410e+05,
         1.46143888e+01,  9.53013920e+04,  1.33963997e+01,
         7.42574771e+04,  1.04791841e+01,  6.58013341e+04,
         8.95530786e+00],
       [-3.48237530e+02, -2.10321296e+05,  8.96417015e+04,
        -7.14038524e+03,  1.92364617e+04,  4.99000202e+03,
         1.10082611e+04,  1.34941127e+03,  2.41927165e+04,
         3.26733542e+00,  2.31011986e+04,  3.22432044e+00,
         1.88491639e+04,  2.65297382e+00,  1.72802490e+04,
         2.36016813e+00],
       [ 1.77139942e+05, -7.71198259e+05,  1.12240864e+05,
        -7.25370986e+04,  5.66130910e+04,  1.10082611e+04,
         9.36434428e+04,  1.07348807e+04,  6.09534507e+04,
         3.44072173e+00,  5.90764148e+04,  4.26292063e+00,
         5.10904441e+04,  4.37089791e+00,  5.24285786e+04,
         5.06825219e+00],
       [ 2.10555125e+04, -8.88750203e+04,  1.35483472e+04,
        -8.44842826e+03,  6.70772964e+03,  1.34941127e+03,
         1.07348807e+04,  1.48215248e+03,  2.49002654e+03,
         1.40557890e-01,  5.84713359e+03,  4.21925848e-01,
         7.21719030e+03,  6.17446227e-01,  9.39064037e+03,
         9.07789891e-01],
       [-2.24912032e+05, -1.66150873e+06,  6.10023925e+05,
         4.40874561e+04,  1.07063410e+05,  2.41927165e+04,
         6.09534507e+04,  2.49002654e+03,  5.91760033e+05,
         9.77850970e+01,  0.00000000e+00,  0.00000000e+00,
         0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
         0.00000000e+00],
       [-9.79292472e+01, -3.20782728e+02,  1.26679014e+02,
         2.82933253e+01,  1.46143888e+01,  3.26733542e+00,
         3.44072173e+00,  1.40557890e-01,  9.77850970e+01,
         2.42137019e-02,  0.00000000e+00,  0.00000000e+00,
         0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
         0.00000000e+00],
       [-1.63415352e+05, -1.45257426e+06,  5.65166386e+05,
         2.77238713e+04,  9.53013920e+04,  2.31011986e+04,
         5.90764148e+04,  5.84713359e+03,  0.00000000e+00,
         0.00000000e+00,  4.84422452e+05,  8.24104281e+01,
         0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
         0.00000000e+00],
       [-8.65388775e+01, -2.86060423e+02,  1.21455772e+02,
         2.47986977e+01,  1.33963997e+01,  3.22432044e+00,
         4.26292063e+00,  4.21925848e-01,  0.00000000e+00,
         0.00000000e+00,  8.24104281e+01,  2.11226210e-02,
         0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
         0.00000000e+00],
       [-8.10556705e+04, -1.10641471e+06,  4.43234727e+05,
         7.27381187e+03,  7.42574771e+04,  1.88491639e+04,
         5.10904441e+04,  7.21719030e+03,  0.00000000e+00,
         0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
         3.50093152e+05,  6.00111232e+01,  0.00000000e+00,
         0.00000000e+00],
       [-6.40511456e+01, -2.17539743e+02,  9.80424886e+01,
         1.80784440e+01,  1.04791841e+01,  2.65297382e+00,
         4.37089791e+00,  6.17446227e-01,  0.00000000e+00,
         0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
         6.00111232e+01,  1.57248915e-02,  0.00000000e+00,
         0.00000000e+00],
       [ 1.31499502e+04, -9.34181143e+05,  3.68206009e+05,
        -1.87787106e+04,  6.58013341e+04,  1.72802490e+04,
         5.24285786e+04,  9.39064037e+03,  0.00000000e+00,
         0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
         0.00000000e+00,  0.00000000e+00,  2.83150690e+05,
         4.74239664e+01],
       [-4.80973452e+01, -1.77667406e+02,  8.44106515e+01,
         1.31142301e+01,  8.95530786e+00,  2.36016813e+00,
         5.06825219e+00,  9.07789891e-01,  0.00000000e+00,
         0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
         0.00000000e+00,  0.00000000e+00,  4.74239664e+01,
         1.26116519e-02]]))

和输出C_new矩阵:

C_new = ', array([[ 3.36326384e+06,  3.35606646e+05, -2.46370980e+06,
         8.96417015e+04, -5.97280544e+06,  5.88733451e+05,
         1.35483472e+04,  6.10023925e+05],
       [ 3.35606646e+05,  7.90884228e+04,  2.54997996e+03,
         1.92364617e+04, -9.46833729e+05, -3.26665859e+04,
         6.70772964e+03,  1.07063410e+05],
       [-2.46370980e+06,  2.54997996e+03,  5.39607129e+06,
        -3.48237530e+02,  1.79979372e+06, -1.12590397e+06,
         2.10555125e+04, -2.24912032e+05],
       [ 8.96417015e+04,  1.92364617e+04, -3.48237530e+02,
         4.99000202e+03, -2.10321296e+05, -7.14038524e+03,
         1.34941127e+03,  2.41927165e+04],
       [-5.97280544e+06, -9.46833729e+05,  1.79979372e+06,
        -2.10321296e+05,  1.85207497e+07, -4.86527342e+05,
        -8.88750203e+04, -1.66150873e+06],
       [ 5.88733451e+05, -3.26665859e+04, -1.12590397e+06,
        -7.14038524e+03, -4.86527342e+05,  3.34731505e+05,
        -8.44842826e+03,  4.40874561e+04],
       [ 1.35483472e+04,  6.70772964e+03,  2.10555125e+04,
         1.34941127e+03, -8.88750203e+04, -8.44842826e+03,
         1.48215248e+03,  2.49002654e+03],
       [ 6.10023925e+05,  1.07063410e+05, -2.24912032e+05,
         2.41927165e+04, -1.66150873e+06,  4.40874561e+04,
         2.49002654e+03,  5.91760033e+05]]))

我只想根据行/列的index_reorder向量来交换行/列(即看起来像排列?)。

1 个答案:

答案 0 :(得分:4)

如您所知,问题是index_reorder仅包含重新排序的元素。

解决方案是,将其扩展为所有元素的完整排列。如果元素应该保留在原位,只需在其旧位置写索引,这样它们就可以保留。

例如:

index_reorder = [2, 4, 0, 5, 1, 3, 7, 8]

应转换为:

full_reorder = [2, 4, 0, 5, 1, 3, 7, 8, 6, 9, 10, 11, 12, 13, 14, 15]

请注意,9-> 9,10-> 10,11-> 11...。这样,它们不会移动也不丢失。还有其他full_reorders可以考虑的,它们的选择仅取决于您的偏好。您可能更喜欢的一种是[2, 4, 0, 5, 1, 3, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]。在这里6-> 6,原始排列围绕着它延伸。

第一个示例中给出的更改后的重新排序可以实现如下:

all_indices = np.array(range(16))
other_indices = np.setdiff1d(all_indices, index_reorder)
full_reorder = np.concatenate([index_reorder, other_indices])

然后继续操作:

C_temp = np.copy(C_in)
C_temp = C_temp[:, full_reorder]
C_temp = C_temp[full_reorder, :]