HAO CHEN
HAO CHEN

Reputation: 1319

how to reshape a 3d array with different dimensions?

I have a numpy array as:

y = np.array([[[14,15,16],[24,25,26],[34,35,36],[44,45,46]],
        [[11,21,31],[12,22,32],[13,23,33],[14,24,34]]])

The shape of y is (2,4,3) and I want to reshape it to (4,3,2) like:

[[[14,11],[15,21],[16,31]],
 [[24,12],[25,22],[26,32]],
 [[34,13],[35,23],[36,33]],
 [[44,14],[45,24],[46,34]]]

I tried use y.reshape(4,3,2)) but the results are not what I want.

Upvotes: 1

Views: 75

Answers (1)

sentence
sentence

Reputation: 8933

You can use numpy.moveaxis():

y = np.moveaxis(y,0,2)

and you get:

array([[[14, 11],
        [15, 21],
        [16, 31]],

       [[24, 12],
        [25, 22],
        [26, 32]],

       [[34, 13],
        [35, 23],
        [36, 33]],

       [[44, 14],
        [45, 24],
        [46, 34]]])

Upvotes: 1

Related Questions