I have a simple issue but impossible to fix it. I have a C_temp
16x16 matrix (size = 16
) like this copied from another matrix.
C_temp = np.zeros((size, size))
C_temp = np.copy(C_in)
Then, I have a permutation list (or numpy array, I don't know if it does matter) :
print('index_reorder =', index_reorder)
gives :
index_reorder = ', array([2, 4, 0, 5, 1, 3, 7, 8]))
I would like to do permutations indiced by index_reorder
along x axis
and y 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)
But unfortunately, the new matrix C_new
size is reduced to 8x8.
This is not what I would like to get : I want to keep the same size for C_new
matrix (16x16)
, i.e doing permutations while keeping the entire size of permuted matrix C_temp
.
How to perform this global permutation ?
This is called a "permutation in-place" I believe, isn't it ?
UPDATE 1 : Here is an example of C_in
matrix 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]]))
and the output C_new
matrix :
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]]))
I just want to swap rows/columns (i.e looks like permutations ?) as a function of index_reorder
vector of rows/columns.
As you found out yourself, the problem is, that index_reorder
does only contain the reordered elements.
The solution is, to extend it to a full permutation off all elements. If the elements should stay in place, just write their index at their old position so they will stay.
E.g.:
index_reorder = [2, 4, 0, 5, 1, 3, 7, 8]
should be transformed to:
full_reorder = [2, 4, 0, 5, 1, 3, 7, 8, 6, 9, 10, 11, 12, 13, 14, 15]
Note, that 9->9, 10->10, 11->11.... That way, they are not moved and not lost. There would be other full_reorders
thinkable, and their choice only depends on your preference. One, that you might prefer is [2, 4, 0, 5, 1, 3, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
. Here 6->6 and the original permutation is stretched around it.
The changed reorder as given in the first example can be achieved as followed:
all_indices = np.array(range(16))
other_indices = np.setdiff1d(all_indices, index_reorder)
full_reorder = np.concatenate([index_reorder, other_indices])
And then continue as you have done:
C_temp = np.copy(C_in)
C_temp = C_temp[:, full_reorder]
C_temp = C_temp[full_reorder, :]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With