Programming Previous page   Next Page

Linear Indexing

With MATLAB, you can refer to the elements of a matrix with a single subscript, A(k). MATLAB stores matrices and arrays not in the shape that they appear when displayed in the MATLAB Command Window, but as a single column of elements. This single column is composed of all of the columns from the matrix, each appended to the last.

So, matrix A

is actually stored in memory as the sequence

The element at row 3, column 2 of matrix A (value = 5) can also be identified as element 6 in the actual storage sequence. To access this element, you have a choice of using the standard A(3,2) syntax, or you can use A(6), which is referred to as linear indexing.

If you supply more subscripts, MATLAB calculates an index into the storage column based on the dimensions you assigned to the array. For example, assume a two-dimensional array like A has size [d1 d2], where d1 is the number of rows in the array and d2 is the number of columns. If you supply two subscripts (i, j) representing row-column indices, the offset is

Given the expression A(3,2), MATLAB calculates the offset into A's storage column as (2-1) * 3 + 3, or 6. Counting down six elements in the column accesses the value 5.


Previous page  Matrix Indexing Functions That Control Indexing Style Next page

© 1994-2005 The MathWorks, Inc.