Multi dimensional array in Data Structures
A multi-dimensional array is an array that contains other arrays as its elements. The most common are 2D arrays (matrices) and 3D arrays. They are used to represent data in multiple dimensions, such as grids, tables, and mathematical matrices.
Types of Multi-Dimensional Arrays
1. Two-Dimensional (2D) Array
A 2D array is essentially an array of arrays. It can be visualized as a table with rows and columns. Each element is accessed using two indices: arr[row][col].
# Python — 2D array using nested lists
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(matrix[1][2]) # Output: 6
2. Three-Dimensional (3D) Array
A 3D array is an array of 2D arrays. It can be visualized as a cube or a collection of matrices. Elements are accessed using three indices: arr[x][y][z].
# Python — 3D array using nested lists
cube = [
[
[1, 2],
[3, 4]
],
[
[5, 6],
[7, 8]
]
]
print(cube[1][0][1]) # Output: 6
3. N-Dimensional Arrays
Arrays with more than three dimensions are also possible but are less common. They are used in specialized fields like scientific computing, machine learning, and computer graphics.
Memory Representation
Multi-dimensional arrays are stored in contiguous memory. There are two main ways to map multi-dimensional indices to memory addresses:
Row-Major Order
Elements are stored row by row. This is used in languages like C, C++, and Python (via NumPy).
For a 2D array: arr[rows][cols]
Address = base + (row * cols + col) * element_size
Example: arr[2][3] → row 2, col 3
Column-Major Order
Elements are stored column by column. This is used in languages like Fortran and MATLAB.
For a 2D array: arr[rows][cols]
Address = base + (col * rows + row) * element_size
Creating Multi-Dimensional Arrays
In Python (using lists)
# 2D array with 3 rows and 4 columns
rows, cols = 3, 4
matrix = [[0 for _ in range(cols)] for _ in range(rows)]
# Access and modify
matrix[0][1] = 5
# Iterate through 2D array
for i in range(rows):
for j in range(cols):
print(matrix[i][j], end=" ")
print()
In Python (using NumPy)
import numpy as np
# 2D array
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
print(arr_2d.shape) # Output: (2, 3)
# 3D array
arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr_3d.shape) # Output: (2, 2, 2)
In Java
// 2D array
int[][] matrix = new int[3][4];
matrix[0][1] = 5;
// 3D array
int[][][] cube = new int[2][3][4];
Common Operations
Traversal
def traverse_2d(matrix):
for i in range(len(matrix)):
for j in range(len(matrix[i])):
print(matrix[i][j], end=" ")
print()
Matrix Addition
def matrix_add(A, B):
rows = len(A)
cols = len(A[0])
result = [[0 for _ in range(cols)] for _ in range(rows)]
for i in range(rows):
for j in range(cols):
result[i][j] = A[i][j] + B[i][j]
return result
Matrix Transpose
def transpose(matrix):
rows = len(matrix)
cols = len(matrix[0])
result = [[0 for _ in range(rows)] for _ in range(cols)]
for i in range(rows):
for j in range(cols):
result[j][i] = matrix[i][j]
return result
Complexity
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Access element | O(1) | O(1) |
| Traversal (2D) | O(n × m) | O(1) |
| Matrix Addition | O(n × m) | O(n × m) |
| Matrix Transpose | O(n × m) | O(n × m) |
| Matrix Multiplication | O(n × m × p) | O(n × p) |
Applications
- Image Processing: 2D arrays represent pixel grids; 3D arrays represent RGB color channels.
- Scientific Computing: Matrices for linear algebra, physics simulations.
- Game Development: Game boards (chess, tic-tac-toe), 3D worlds.
- Machine Learning: Feature matrices, tensors for deep learning.
- Spreadsheets: Excel-like data tables.
- Computer Graphics: 3D transformations, textures.
- Geographic Information Systems (GIS): Grid-based terrain data.
Advantages & Disadvantages
Advantages
- Organizes data in a structured, intuitive way.
- Efficient random access (O(1)).
- Cache-friendly when traversed in the correct order.
Disadvantages
- Fixed size in many languages (unless using dynamic structures).
- Memory overhead for large dimensions.
- Can be difficult to visualize beyond 3 dimensions.
Ready to master Data Structures & Algorithms?
Learn DSA hands-on with mentor-led sessions, real interview practice, and placement support.
.png)