Everything in MATLAB is a matrix. A vector is a 1xN or Nx1 matrix; a scalar is a 1x1 matrix. The most straightforward way to make a vector or matrix is to write it out:

`.1ex>> v = [1 2 3 4 5]`
creates the obvious vector (the spaces may be replaced with commas);
the statements

`.1ex>> m = [1 2 3; 4 5 6; 7 8 9]`
and

.1ex>>B<>1 2 3m = [

4 5 6

7 8 9]

both create the obvious matrix. Note that semicolons are used to separate the rows of the matrix in the one-line square bracket notation.

It can be convenient to enter a complex-valued matrix or vector in the form

`.1ex>> m = [1 2; 3 4] + i [4 3; 2 1]`
instead of the equivalent

`.1ex>> m = [1+4i 2+3i; 3+2i 4+i]`
There are a number of useful functions to create a matrix from scratch.
Specifically,

The function **size**
takes a matrix and returns the vector [*width height*]; this can
be used with other functions to build matrices the same size as existing ones,
e.g.

`.1ex>> emptym = zeros(size(m))`
The

Matrices and vectors may be built out of smaller ones;

`.1ex>> va = [1 2 3]; vb = [4 5 6]; vc = [7 8 9];`

produces the same matrix as before. Similarly, matrices can be built in blocks:

`.1ex>> ma = [1 2; 3 4]`

Since the size of a matrix can be changed at any time (and can be zero), things like this are valid:

`.1ex>> x = [ ];`

Note that resizing a matrix does take time; doing it ten thousand times in a loop can be noticeable. A way to avoid this is to start with a matrix of zeros (or ones, as appropriate) of the eventual largest size, and then to use only part of the matrix at a time.

There are operations specifically to let you get a new object
from an existing one; **triu** and **tril** take a matrix and
produce its upper and lower triangular matrices. **Diag** takes
a matrix and produces the vector of its diagonal entries, or takes
a vector and produces a diagonal matrix with those entries. **fliplr**
and **flipud** take a matrix and flip it left-right or up-down; **rot90**
takes a matrix and rotates it counterclockwise 90 degrees (an optional integer
second argument specifies repetition). The operator **'** takes the
conjugate transpose of a matrix; the operator **.'** takes the
transpose. The **reshape** function rearranges matrix elements into
a new shape.

`.1ex>> [1 2+i]'`

Elements of a matrix are referenced as *m*(*row*, *column*) (or *v*(*n*) for
a vector); this can be used to access elements, or to build a matrix.
Indexing starts at one, not at zero, so it looks like

You could, painfully, build a matrix like

`.1ex>> m(1, 1) = 1; m(1, 2) = 2; m(2, 1) = 3; m(2, 2) = 4`

but this is more useful used in conjuction with **for** and **while**
statements (see ** Flow**). On the other hand, submatrices
can often be done this way quickly:

`.1ex>> m = [1 2 3; 4 5 6; 7 8 9]; s = m([1 2], [2 3])`

Also see **help colon**.

`.1ex>> v = 1:5`

Finally, matrices can be loaded from a disk file created via MATLAB or
some other application (e.g. a text editor); this is the fastest
method for large matrices. If a file named *base.extension*
contains a series of rows of numbers (no other formatting) defining a
matrix, **load base.extension** will assign that matrix to the
variable *base*. E.g. **load values.txt** takes data from the
file **values.txt** and puts it into **values**. Don't use a **
.mat** extension, since that will make **load** assume it is a
MATLAB-saved file. Since **load** is frequently used for very large
matrices, it does not echo the input.

Sat Mar 21 21:42:28 EST 1998