Python: Row and Column Major Multidimensional Array

Linux School Tech
3 min readJul 26, 2024

--

Image Source: https://craftofcoding.wordpress.com/

In computer science, the terms row-major order and column-major order refer to the ways of storing multidimensional arrays in linear memory. Understanding these concepts is crucial for optimizing performance and interfacing with libraries or systems that expect a specific memory layout.

Row-Major vs. Column-Major Order

Row-Major Order

In row-major order, the consecutive elements of the rows of the array are contiguous in memory. This means that elements of the first row are stored first, followed by the elements of the second row, and so on.

Column-Major Order

In column-major order, the consecutive elements of the columns of the array are contiguous in memory. This means that elements of the first column are stored first, followed by the elements of the second column, and so on.

Python and NumPy

Python’s NumPy library is widely used for numerical computations and supports both row-major and column-major order through the order parameter in array creation and manipulation functions.

NumPy, uses a default layout that is equivalent to row-major order in other languages like C.

Examples

Creating Arrays in Row-Major Order

In row-major order, the elements are arranged such that all the elements in a single row are stored contiguously in memory before moving on to the next row.

This means that if you have a 2D array with dimensions (m, n), then the element at position (i, j) will be located at index i*n + j in a flattened version of the array.

import numpy as np

# Create a 2D array with shape (3, 4)
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(arr)

# Flatten the array to 1D and print its contents
flat_arr = arr.flatten()
print(flat_arr)

# Access an element by specifying its row and column indices
elem = arr[1, 2]
print(elem)

Output:

[[ 1  2  3  4]
[ 5 6 7 8]
[ 9 10 11 12]]
[ 1 2 3 4 5 6 7 8 9 10 11 12]
7
Row-Major Order

As you can see from the output, the elements in each row are stored contiguously in memory. The element at position (1, 2) has a value of 7, which matches our manual calculation based on the formula for accessing elements in row-major order.

Creating Arrays in Column-Major Order

If you want to explicitly specify column-major order when creating a NumPy array, you can use the order parameter and set it to 'F' in the flatten() function.

import numpy as np

# Create a 2D array with shape (3, 4) in column-major order
arr_f = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(arr_f)

# Flatten the array to 1D and print its contents
flat_arr_f = arr_f.flatten('F')
print(flat_arr_f)

Output:

[[ 1  2  3  4]
[ 5 6 7 8]
[ 9 10 11 12]]
[ 1 5 9 2 6 10 3 7 11 4 8 12]
Column-Major Order

In this case, the elements are arranged in column-major order, so the values within each column are stored contiguously in memory instead. To access an element at position (i, j), you would use the formula i + j*m in a flattened version of the array.

Read More

My YouTube Channel

More shell script videos and linux tutorials on my YouTube Channel.

--

--