Warat
Warat

Reputation: 29

How to swap 2 dimensions in an array

I have a 4 dimensionnal set of data coming from a Tiff file.

image_stack = io.imread(path, plugin='tifffile')
print(image_stack.shape)
>>> (21, 10, 1331, 1126) 

So the last 2 Dimensions are the images resolutions. The 10 is because I have 10 slices of the same image (over the z axis) at a given time And 21 because all of those images are taken every second.

How can I swap the 21 and 10 dimensions ?

Upvotes: 2

Views: 823

Answers (1)

yatu
yatu

Reputation: 88236

You can just swap the two first axes with swapaxes:

a = np.random.rand(21, 10, 1331, 1126)
a.swapaxes(1,0).shape
# (10, 21, 1331, 1126)

Or move the second axis backwards with rollaxis:

np.rollaxis(a, 1).shape
# (10, 21, 1331, 1126)

Upvotes: 3

Related Questions