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

Reordering matrix with a permutation vector but keeping the original size of matrix

我有一个简单的问题,但无法解决。我有一个从另一个矩阵复制而来的 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) 保持相同的大小,即在保持置换矩阵 [=15= 的整个大小的同时进行排列].

如何执行这个全局排列?

这叫做 "permutation in-place" 我相信,不是吗?

更新 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]]))

我只想交换 rows/columns(即看起来像排列?)作为 rows/columns 向量的函数 index_reorder

正如您自己发现的那样,问题是 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, :]