1

I got a 3d-array

>>> a
array([[[[9, 6, 1],
         [2, 8, 6],
         [3, 5, 6]],

        [[9, 1, 9],
         [6, 6, 7],
         [3, 0, 7]],

        [[9, 2, 7],
         [6, 1, 4],
         [9, 2, 2]],

        [[3, 7, 0],
         [4, 0, 6],
         [7, 4, 8]]]]) 

Now I want to reshape that array, that specific values are grouped together, i.e.

 >>> a[0,:,0,0]
 array([9, 9, 9, 3])

So basically, I want the first element of each box to be in one array, the second element of each box in another array and so on.

What I did so far is reshape it to the following:

>>> b = a[0,:,:,:].T
>>> b
array([[[9, 9, 9, 3],
        [2, 6, 6, 4],
        [3, 3, 9, 7]],

       [[6, 1, 2, 7],
        [8, 6, 1, 0],
        [5, 0, 2, 4]],

       [[1, 9, 7, 0],
        [6, 7, 4, 6],
        [6, 7, 2, 8]]])

This way the arrays get constructed already but the sorting is wrong. This way the 2x4 matrices are constructed row-wise. I want it to be constructed column-wise, that is, in this case, the following should be the first six rows:

>>> b[:,0,:]
array([[9, 9, 9, 3],
       [6, 1, 2, 7],
       [1, 9, 7, 0]])

>>> b[:,1,:]
array([[2, 6, 6, 4],
       [8, 6, 1, 0],
       [6, 7, 4, 6]])

I've been fiddling around with how to index that my array is constructed the right way. An intuitive way of thinking about the problem is considering the 3x3 matrices are stacked behind one another and I want to carve out 1x4 arrays going in z-direction (so into the pile) and put them into one array row-wise.

I mean I could iterate over them with a for-loop, but I'd rather vectorize the whole thing by indexing and slicing. Any help is appreciated! Thanks

np.concatenate([b[:,i,:] for i in range(3)])
2
  • Note that you here have a 4d array, not a 3d array. Commented Nov 1, 2018 at 14:21
  • Yes, I was aware of that and dropped it eventually, but thank you for pointing it out. Commented Nov 1, 2018 at 14:26

1 Answer 1

3

numpy.transpose(..) is a function that can permutate the axes in any order.

If I understood it correctly, you basically want the third and fourth axis to be the first and second axis of the new matrix, and the old first and second matrix to be the new third and fourth axis.

We can thus make such tranpose with:

>>> a.transpose((2,3,0,1))
array([[[[9, 9, 9, 3]],
        [[6, 1, 2, 7]],
        [[1, 9, 7, 0]]],

       [[[2, 6, 6, 4]],
        [[8, 6, 1, 0]],
        [[6, 7, 4, 6]]],

       [[[3, 3, 9, 7]],
        [[5, 0, 2, 4]],
        [[6, 7, 2, 8]]]])

Since here you constructed a 4d array, but the first index has only one element, it might be better to just drop that index, for example with:

>>> a[0,:,:,:].transpose(1,2,0)
array([[[9, 9, 9, 3],
        [6, 1, 2, 7],
        [1, 9, 7, 0]],

       [[2, 6, 6, 4],
        [8, 6, 1, 0],
        [6, 7, 4, 6]],

       [[3, 3, 9, 7],
        [5, 0, 2, 4],
        [6, 7, 2, 8]]])
Sign up to request clarification or add additional context in comments.

1 Comment

Aw man! Thanks...I knew there was something wrong with my transposing. That solved my problem and also got rid off my headache. Exactly what I wanted.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.