next up previous
Next: Numbers and Arithmetic Expressions Up: Guide Contents Previous: VariablesExpressions and Statements

Matrices and MATLAB

Essentially, the only data objects in MATLAB are rectangular numerical matrices. There are no restrictions placed on the dimensions of a matrix (except by system resources), but special meaning is sometimes attached to tex2html_wrap_inline451 matrices (scalars) and matrices with only one row or column (vectors). The memory required for the storage of each matrix is automatically allocated by MATLAB.

The easiest way to enter a matrix into MATLAB is to provide an explicit list of elements enclosed in square brackets [ ]. MATLAB uses the following conventions:

For example, entering the assignment statement
>> A = [1 2 4.5; 8/2.0 6 5]
results in the output
A =
    1.0000    2.0000    4.5000
    4.0000    6.0000    5.0000
The tex2html_wrap_inline453 matrix is saved in variable A for future reference. If you want to see the contents of this or any other variable, simply enter its name as a command. To reference individual elements enclose their subscripts in parentheses after the variable name in the usual fashion.
>> A(2,3)
ans =
     5

It is important to realize that MATLAB distinguishes between row and column vectors. [1 2 3] is a row vector, while [1; 2; 3] is a column vector. Column vectors can also be created by applying MATLAB's transpose operator ' (prime) to a row vector. For example
>> [1 2 3]'
produces

ans =
     1
     2
     3
The transpose operator may be applied to matrices of any dimension.



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