0

I have an array with the shape of TxCxHxW, how can I change the order inside a channel.

For example, I have a and like to change it so I get b as following (please note that it is just an example, in general, I am looking for a way fast to change the order inside the channel, The change of order is not random and we always have the pattern):

a = np.arange(48).reshape(4,3,2,2)
b = np.zeros_like(a)
b[0,:,:,:] =a[1,:,:,:]
b[1,:,:,:] =a[3,:,:,:]
b[2,:,:,:] =a[0,:,:,:]
b[3,:,:,:] =a[2,:,:,:]
Alex
  • 1,194
  • 1
  • 12
  • 20
  • How do you want to change the order though. If you want to randomly shuffle, you could use `np.random.shuffle` or other way would be to create a 1-D np array of indices and just do `b = a[required_indices_arr]`. – Anurag Reddy Aug 21 '20 at 15:30
  • it is not random, I always have a pattern. can you say how you can do it for my example. – Alex Aug 21 '20 at 15:43
  • So, what's **the pattern** for a generic case with n elements? – Divakar Aug 21 '20 at 15:44
  • let say the pattern is what I showed in the example – Alex Aug 21 '20 at 15:45

2 Answers2

2

To get the same result as your code, you can do something like:

a = np.arange(48).reshape(4,3,2,2)
b = a[[1,3,0,2], :, :]
Bas
  • 153
  • 5
1

You may need this: note:second row([0, -2, -1, -3]) is the distance between original dimension of target dimension

# convert numpy matrix dimension [N, H, W, C] -> [N, C, H, W]
np_train_samples = np.moveaxis(np_train_samples, [0, 1, 2, 3],
                                   [0, -2, -1, -3])
Simon
  • 161
  • 1
  • 14