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.

2
  • [0 12 14], [1,13,25], [2,24,26] ..... etc is this output correct? Commented Mar 9, 2018 at 1:58
  • You may need to transpose some axes. That's what the linked answer does. Commented Mar 9, 2018 at 3:02

1 Answer 1

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]])
Sign up to request clarification or add additional context in comments.

1 Comment

Thats it! Thanks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.