next up previous
Next: For LoopsWhile Loops, Up: Guide Contents Previous: Array Operations

Vector and Matrix Manipulation

Vectors are easily generated with MATLAB's colon ``:'' notation. For example, the expression 1:5 creates the following row vector.
1 2 3 4 5
You can also create a vector using an increment other than one. For example, 1:2:7 results in the vector
1 3 5 7
The increment may be negative and need not be an integer.

It is very easy to create a table using the colon notation. Experiment with the commands
>> x=(0:pi/4:pi)'; y=cos(x); AA=[x y]
for a demonstration of this technique.

MATLAB permits users to easily manipulate the rows, columns, submatrices and individual elements of a matrix. The subscripts of matrices can be vectors themselves. If x and v are vectors then x(v) is equivalent to the vector [x(v(1)), x(v(2)), tex2html_wrap_inline461 ] . Subscripting a matrix with vectors extracts a submatrix from it. For example, suppose A is an tex2html_wrap_inline483 matrix. A(1:4, 5:8) is the tex2html_wrap_inline485 submatrix extracted from the first 4 rows and last 3 columns of the matrix. When the colon operator is used by itself, it denotes all of the rows or columns of a matrix. Using the result of the table above
>> AA(:,1)
produces the first column of matrix AA.

ans =
            0
   7.8540e-01
   1.5708e+00
   2.3562e+00
   3.1416e+00



Ian Cavers
Fri Dec 4 15:01:52 PST 1998