0

As a disclaimer I'm very new to python and numpy arrays. Reading some of the answers to similar questions and trying their solutions for my own data hasn't been very helpful so I thought I'd just post my own question. For example, Reshaping 3D Numpy Array to a 2D array. Its entirely believable though that I've just implemented those other solutions wrong.

I have a 3D numpy array "C"

C = np.reshape(np.arange(3*3*4),(3,3,4))
print(C)
[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]

 [[24 25 26 27]
  [28 29 30 31]
  [32 33 34 35]]]

I would like to reshape it into something like:

[0 12 14], [1,13,25], [2,24,26] ..... etc 

where the first elements of each of the 3 arrays gets put into its own array, then the second elements of each array get put into a new array, and so on.

It seems trivial, but I'm stumped. I've tried different types combinations of .reshape, just for example,

output=C.reshape(12,3)

I've tried changing the order from "C" to "F", playing around with different .reshape() parameters, but can't seem to actually get the final result in the desired structure

Any tips would be much appreciated.

Harry B
  • 351
  • 1
  • 5
  • 17

1 Answers1

2

I think this is what you want:

C = np.reshape(np.arange(3*3*4),(3,3,4))
C.reshape(3,12).T

array([[ 0, 12, 24],
       [ 1, 13, 25],
       [ 2, 14, 26],
       [ 3, 15, 27],
       [ 4, 16, 28],
       [ 5, 17, 29],
       [ 6, 18, 30],
       [ 7, 19, 31],
       [ 8, 20, 32],
       [ 9, 21, 33],
       [10, 22, 34],
       [11, 23, 35]])
Julien
  • 13,986
  • 5
  • 29
  • 53