Addressing formula of (i,j)th element of a m*n matrix in Column-major order

In this article we will learn how to calculate address of (i,j)th element of a matrix m*n in Column major order. Below is the explanation of it.

Column Major Order Formula

The Location/Address of element A[i, j] can be obtained by evaluating expression:

LOC (A [i, j]) = base_address + w * [m * j + i]

Here,

Base_Address = Address of the first element in the array.

w(Size of element)= Word size means a number of bytes occupied by each element of an Array. It depends on the data type, e.g., 4 bytes for an int in many systems.

m = Number of rows in the array.

i = is the row index

j = is the column index

Here we have taken a 2-D array A [2, 4] which has 2 rows and 4 columns.

Explanation:

  1. Column Index Multiplied by Row Count: The term (M * j) finds the starting position of the jth column. Since it’s column-major, to get to the start of the jth column, we skip j columns, each of which has m elements.
  2. Row Index Addition: Adding the row index i moves down to the ith element in that column.
  3. Scaling by Element Size: Multiplying by w(size of element) adjusts the address according to the actual memory size of each element in the matrix.
  4. Adding Base Address: Finally, adding the Base_Address of the matrix gives the absolute memory address of the element A[i, j].

Problem to solve on Column Major Order

Now to calculate the base address of any index using column major order we can use process given below.

Consider a 4×3 (m=4, n=3) matrix stored in column-major order starting at base address 2000, and assume each element is an integer type taking 4 bytes. To find the address of the element in the second row and third column (A[1, 2]):

It can be calculated as follow:
Here,
Given base_address = 2000, w(Size)= 4, m(number of rows)=4, i(row index)=1, j(column index)=2
LOC (A [i, j]) = base_address + W [m * j + i ]
LOC (A[1, 2]) = 2000 + 4 *[4 * 2 + 1]
= 2000 + 4 * [8 + 1]
= 2000 + 4 * 9
= 2000 + 36
= 2036