Next  |  Prev  |  Top  |  JOS Index  |  JOS Pubs  |  JOS Home  |  Search

Rows, columns and submatrices

Individual elements of a matrix can be accessed by giving their subscripts in parentheses, eg. A(1,2), x(i), TAB(ind(k)+1). An expression used as a subscript is rounded to the nearest integer.

Individual rows and columns can be accessed using a colon ':' (or a '|') for the free subscript. For example, A(1,:) is the first row of A and A(:,j) is the j-th column. Thus

   A(i,:) = A(i,:) + c*A(k,:)
adds c times the k-th row of A to the i-th row.

The colon is used in several other ways in MATLAB, but all of the uses are based on the following definition.

   j:k    is the same as  <j, j+1, ..., k>
   j:k    is empty if  j > k .
   j:i:k  is the same as  <j, j+i, j+2i, ..., k>
   j:i:k  is empty if  i > 0 and j > k or if i < 0 and j < k .
The colon is usually used with integers, but it is possible to use arbitrary real scalars as well. Thus
   1:4  is the same as  <1, 2, 3, 4>
   0: 0.1: 0.5 is the same as <0.0, 0.1, 0.2, 0.3, 0.4, 0.5>

In general, a subscript can be a vector. If X and V are vectors, then X(V) is <X(V(1)), X(V(2)), ..., X(V(n))> . This can also be used with matrices. If V has m components and W has n components, then A(V,W) is the m by n matrix formed from the elements of A whose subscripts are the elements of V and W. Combinations of the colon notation and the indirect subscripting allow manipulation of various submatrices. For example,

   A(<1,5>,:) = A(<5,1>,:)  interchanges rows 1 and 5 of A.
   A(2:k,1:n)  is the submatrix formed from rows 2 through k
      and columns 1 through n of A .
   A(:,<3 1 2>)  is a permutation of the first three columns.

The notation A(:) has a special meaning. On the right hand side of an assignment statement, it denotes all the elements of A, regarded as a single column. When an expression is assigned to A(:), the current dimensions of A, rather than of the expression, are used.


Next  |  Prev  |  Top  |  JOS Index  |  JOS Pubs  |  JOS Home  |  Search