Ziyuan
Ziyuan

Reputation: 4558

How to roll the axis forwards in numpy?

numpy.rollaxis rolls the axis backwards. Suppose I have an ndarray with the shape (3,640,480) and I want to roll the first axis to the last, changing the shape to (640,480,3). Can I do this easily? Although in this simple case two calls of numpy.rollaxis will work, if I have n axis this will be clumsy.

Upvotes: 3

Views: 641

Answers (1)

TheBlackCat
TheBlackCat

Reputation: 10298

There is an optional third argument, start, which tells where the axis should end up. In this case, you want to move axis 0 before axis 3:

>>> x.shape
(3,640,480)
>>> x2 = np.rollaxis(a,0,3)
>>> x2.shape
(640,480,3)

Upvotes: 4

Related Questions