# Matlab Vectors and Matrices

Matlab stands for "Matrix Laboratory", so it is not a surprise
that Matlab has simple, intuitive, but powerful matrix structures.
The general term for such structures is *array*, but to retain
the link to mathematics we will refer to vectors and matrices.
A vector is a collection of values indexed using a single integer, while
a matrix is a collection of values indexed by an order pair of integers.
We have already seen the most simpleminded way to enter a vector:

v = [1 2 3 4 5 6 7 8 9 10]

However, while simpleminded, this is not the simplest way to enter that vector. The following is a completely equivalent statement in Matlab.

v = 1:10

The colon is always used to denote a range in Matlab. In this case,
`1:10` indicates to Matlab to form a vector starting with the value
1, incrementing in steps of 1 (this is not stated, it is the default), up
to 10. If we wanted instead to form the vector `w=[1 3 5 7 9]`
we could use the syntax

w = 1:2:9

which indicates that Matlab should form a vector starting with 1, incrementing
by 2, up to 9. Note that we could accomplish the same thing using the
command `w = 1:2:10.5`. Thus, we see that the syntax
*a:b:c* in Matlab represents *form a vector starting with a,
incrementing by b, up to the number less than or equal to c.*
Note that none of the numbers *a, b* or *c* are required
to be integers.

Now that we have vectors, we might have occasion to refer to just one
of their entries. This can be done using the index inside parentheses.
Using the example vectors above, we can refer to the third entry in
`v` by typing `v(3)`; the result is 3. Likewise we can
get the third entry in `w`: `w(3)` would be 5. We can set
the values of individual entries in vectors in the same way.

w(3) = 293 w = 1 3 293 7 9

We can go further. Suppose we want to change the first, second, and third
entries in `w`. We could make three lines giving e.g.
`w(1) = 11; w(2) = 22; w(3) = 33;`, but we could also give
a vector as the index.

w(1:3) = [11 22 33] w = 11 22 33 4 5

Matrices behave in a very similar way, but in this case the indices are ordered pairs of integers. Consider

A = [1 2 3; 4 5 6; 7 8 9] A = 1 2 3 4 5 6 7 8 9

We could accomplish this same assignment using the command
`A = [1:3;4:6;7:9]`. One would not want to do this, but it
is instructive to note that we could also do this assignment
as `A=[[1:3:7]' [2:3:8]' [3:3:9]']`. In the last case,
we form the matrix as a collection of column vectors.
Now we can refer to the 1 in the matrix as `A(1,1)`; the
3 is `A(1,3)`; the 7 is A(3,1). We could extract the
2×2 submatrix in the upper right corner of A using

B = A(1:2,2:3) B = 2 3 5 6

In the same way one can use vector indices to assign entire rows or columns, as in the following.

A(:,3) = [0 0 0]' A = 1 2 0 4 5 0 7 8 0

Note that the notation `A(:,3)` indicates that the assignment
is being done to the entire third column of A. Thus, the above example
says: "replace the entire third column of A with the transpose of a
zero row vector of dimension 3."

A solution for the
final is available.