In a 3D array, elements are organized in three dimensions, and their positions are specified using three indices, typically written as [i][j][k] or (i, j, k). Here’s a breakdown:
How Indexing Works:
This hierarchy lets you pinpoint any element in the 3D array.
How Indexing Works:
- First Dimension (
i):
- Determines which “block” or “layer” of the array you’re accessing.
- Think of it as choosing a specific 2D “sheet” within the 3D array.
- Second Dimension (
j):
- Specifies the “row” within the selected 2D sheet.
- Third Dimension (
k):
- Points to the “column” within the chosen row.
This hierarchy lets you pinpoint any element in the 3D array.
Example:
Imagine a 3D array as a stack of 2D grids:
Layer 0:
[[1, 2, 3],
[4, 5, 6]]
Layer 1:
[[7, 8, 9],
[10, 11, 12]]
- To access the number
9, you’d use indices[1][0][2]: 1: Select the second layer (index 1 because indexing starts at 0).0: Within that layer, pick the first row.2: Then, take the third element in that row.
In Code (Using NumPy):
import numpy as np
# Create a 3D array
array = np.array([[[1, 2, 3], [4, 5, 6]],
[[7, 8, 9], [10, 11, 12]]])
# Access element 9
element = array[1, 0, 2]
print(element) # Output: 9
This structure makes it easy to navigate, slice, and manipulate data in three-dimensional space.