简体   繁体   中英

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM