Skip to content

Instantly share code, notes, and snippets.

@pure-rgb
Forked from innat/4Data.md
Created November 24, 2018 15:50
Show Gist options
  • Save pure-rgb/dcc6f5e220349bd9f0f509666eccaa3e to your computer and use it in GitHub Desktop.
Save pure-rgb/dcc6f5e220349bd9f0f509666eccaa3e to your computer and use it in GitHub Desktop.
4D - Four Dimensional Data

Let's consider a folowing 4D matrix.

import numpy as np
W = np.random.randn(2,2,3,100)

print(W.shape)

output:
(2, 2, 3, 100)

The first three axes represent three dimensional space. The last axis represents time. Here the last (time) axis has length 100. This means that, for each of these 100 elements, there is one whole three dimensional image. We often call the three-dimensional images volumes. So we could say that this 4D image contains 100 volumes.

We can now take slices over the fourth axis of this four-dimensional matrix:

first_volume = W[:, :, :, 0]

pirnt(first_volume.shape)

output:
(2, 2, 3)

This slice selects the first three-dimensional volume (3D image) from the 4D array. We can use the ellipsis ... when slicing an array. The ellipsis is a short cut for everything on the previous axes. For example, these two have exactly the same meaning:

first_volume = W[:, :, :, 0]
first_volume = W[..., 0] 

first_volume is a 3D matrix:

first_volume.ndim # 3 - three dimentional 

Check this for more information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment