python - exchange columns in numpy matrix -


i have numpy matrix of shape m,n. now, want exchange first column last, second column second last, third column third last , on..

is there "numpy" way this?

right now, looping thru half of num_cols , exchanging columns.

use numpy slicing reverse column order in array:

my_array = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) print(my_array[:, ::-1]) 

output

[[ 4  3  2  1]  [ 8  7  6  5]  [12 11 10  9]  [16 15 14 13]] 

a typical slice of form start:stop:step. x-dimension slice, :, default, selects all of rows. y-dimension slice, ::-1, selects of columns, step size of -1, places them in reverse order, hence swapping order of columns desired.


Comments