Which of the following expressions accesses the (i, j)th entry of a (m...
(i, j) entries in column major order of size (m x n).
Assume starting address is 1 so, m x (j - 1) + i
View all questions of this test
Which of the following expressions accesses the (i, j)th entry of a (m...
Explanation:
In column major form, the elements of a matrix are stored column-wise. This means that elements of each column are stored consecutively in memory.
To access the (i, j)th entry of a (m x n) matrix stored in column major form, we need to calculate the correct index in the memory.
Let's consider a (3 x 4) matrix as an example:
| a11 | a21 | a31 |
|-----|-----|-----|
| a12 | a22 | a32 |
| a13 | a23 | a33 |
| a14 | a24 | a34 |
The elements are stored column-wise in memory as follows:
[a11, a12, a13, a14, a21, a22, a23, a24, a31, a32, a33, a34]
To calculate the index:
- The number of columns in the matrix is n.
- The number of elements in each column is m.
So, to access the (i, j)th entry, we need to calculate the index as:
index = (j-1) * m + (i-1)
Explanation of the expressions:
- Option a) n x (i-1) + j: This expression calculates the index in row major form, not column major form.
- Option b) m x (j-1) + i: This expression correctly calculates the index in column major form.
- Option c) m x (n-j) + i: This expression calculates the index in row major form, not column major form.
- Option d) n x (m-i) + j: This expression calculates the index in row major form, not column major form.
Therefore, the correct expression to access the (i, j)th entry of a (m x n) matrix stored in column major form is m x (j-1) + i.