Linear algebra · indexing
Elements of a Matrix: Subscripts, Slices and Linear Indices
An element of a matrix is one of its entries, identified by two numbers: the row it sits in and the column it sits in. The element in row i and column j is written aij in mathematics and A(i, j) in most matrix languages. Everything else about indexing, from slicing out a column to deleting a row, builds on that pair of subscripts.
Subscripts and the order of the pair
Row first, column second, always. For the matrix
A = [1 2 3; 4 5 6; 7 8 9]
the element A(2, 3) is 6: second row, third column. Swapping the pair gives A(3, 2) = 8, a different element. A matrix with m rows and n columns has m·n elements, and the valid subscripts run from 1 to m for the row and 1 to n for the column in one-based languages.
Zero-based languages such as NumPy shift everything down by one, so the same element is A[1, 2]. This single difference accounts for a large share of porting bugs. A loop that runs “for i from 1 to n” in one language becomes “for i from 0 to n − 1” in the other, and an off-by-one at either end quietly reads the wrong entry or fails with an out-of-range error.
Slices: many elements at once
A colon stands for “every index along this direction”. A(2, :) is the whole second row, [4 5 6], and A(:, 3) is the whole third column, [3; 6; 9]. A range such as A(1:2, 2:3) picks the block [2 3; 5 6]. In MATLAB-style languages both ends of a range are included, so 1:2 means rows 1 and 2. NumPy slices exclude the stop index, so the same block is A[0:2, 1:3].
Index lists work as well. A([1 3], :) returns rows 1 and 3 stacked, which is how you reorder or repeat rows. A logical mask, a matrix of true and false values of the same shape, picks out exactly the elements marked true and returns them as a column. Writing A(A > 5) gives (7, 8, 6, 9) in column order for the matrix above, which brings up the next topic.
Linear indexing and storage order
A matrix lives in memory as one long block, and it can be addressed with a single index. MATLAB-style languages store column by column (column-major order), so for an m × n matrix the element A(i, j) has linear index (j − 1)·m + i. For A above, A(2, 3) sits at position (3 − 1)·3 + 2 = 8, and counting down the columns, 1, 4, 7, 2, 5, 8, 3, 6, 9, the eighth value is indeed 6.
C and NumPy default to row-major order, where the index is (i − 1)·n + j instead. Here that gives (2 − 1)·3 + 3 = 6, and the sixth value reading across rows, 1 through 9, is again 6. The same element, a different position in the flattened list. The distinction matters whenever you reshape, flatten or hand data to code in another language: a reshape that fills column by column yields a different matrix than one that fills row by row.
A = [1 2 3; 4 5 6; 7 8 9];
A(2,3) % 6
A(8) % 6, linear index, column-major
A(:, 2) % [2; 5; 8]
A(end, end) % 9, the last element
Assigning and growing
Indexing works on the left of an equals sign too. A(2, :) = 0 zeroes the second row. Assigning outside the current size, A(4, 4) = 1, grows the matrix in many languages and pads the new positions with zeros. That is convenient in a quick session and a performance trap inside a loop, since each growth may copy the whole array. Preallocating the final size once is the better habit.
Deleting an entry is only possible for whole rows or columns, because a matrix has to stay rectangular. A(2, :) = [] removes the second row in MATLAB-style syntax.
Where to go next
Entries combine through element-by-element multiplication, and the cumulative operations along a column are covered in cumulative product. Terms like column-major and submatrix are in the glossary, and the linear algebra hub has the neighboring topics.