8.7: Vectors and Matrices
As its name indicates, MATLAB is especially designed to handle matrices. The simplest way to enter a matrix is to use an explicit list. In the list, the elements are separated by blanks or commas, and the semicolon (;) is used to indicate the end of a row. The list is surrounded by square brackets [ ]. For example, the statement
≫ A = [1 2 3;4 5 6;7 8 9]
results in the output
A = 1 2 3 4 5 6 7 8 9
The variable A is a matrix of size 3×3. Matrix elements can be any MATLAB expression. For example, the command
≫ x = [-1.3 sqrt(3) (1+2+3)*4/5]
results in the matrix
x = -1.3000 1.7321 4.8000
We call a matrix with just one row or one column a vector, and a 1×1 matrix is a scalar. Individual matrix elements can be referenced with indices that are placed inside parentheses. Type
x(5) = abs(x(1))
to produce the new vector
x = -1.3000 1.7321 4.8000 0.000 1.3000
Note that the size of
x
has been automatically adjusted to accommodate the new element and that elements not referenced are set equal to 0 (here x(4)). New rows or columns can be added very easily. Try typing
r = [10 11 12],A = [A;r]
. Dimensions in the command must coincide. Try
r = [13 14],A = [A;r]
.
The command
size(A)
gives the number of rows and the number of columns of
A
. The output from
size(A)
is itself a matrix of size 1×2. These numbers can be stored if necessary by the command
[m n] = size(A)
. In our previous example,
A = [A;r]
is a 4×3 matrix, so the variable
m
will contain the number 4 and
n
will contain the number 3. A vector is a matrix for which either
m
or
n
is equal to 1. If
m
is equal to 1, the matrix is a row vector; if
n
is equal to 1, the matrix is a column vector. Matrices and vectors may contain complex numbers. For example, the statement
≫ A = [1 2;3 4]+j*[5 6;7 8]
and the statement
≫ A = [1+5*j 2+6*j;3+7*j 4+8*j]
are equivalent, and they both produce the matrix
A = 1.0000+5.0000i 2.0000+6.0000i 3.0000+7.0000i 4.0000+8.0000i
Note that blanks must be avoided in the second expression for
A
. Try typing
≫A = [1 + 5*j 2 + 6*j 2 + 6*j;3 +7*j 4 + 8*j]
What is the size of
A
now?
MATLAB has several built-in functions to manipulate matrices. The special character, ', for prime denotes the transpose of a matrix. The statement
A = [ 1 2 3;4 5 6;7 8 9]'
produces the matrix
A = 1 4 7 2 5 8 3 6 9
The rows of
A'
are the column of
A
, and vice versa. If
A
is a complex matrix, then
A
is its complex conjugate transpose or hermitian transpose. For an “unconjugate” transpose, use the two-character operator dot-prime (. '). Matrix and vector variables can be added, subtracted, and multiplied as regular variables if the sizes match. Only matrices of the same size can be added or subtracted. There is, however, an easy way to add or subtract a common scalar from each element of a matrix. For example,
x = [1 2 3 4],x = x-1
produces the output
x = 1 2 3 4 x = 0 1 2 3
As discussed in
the chapter on linear algebra
, multiplication of two matrices is only valid if the inner sizes of the matrices are equal. In other words,
A*B
is valid if the second size of
A
(number of columns) is the same as the first size of
B
(number of rows). Let ai,jai,j represent the element of
A
in the \(i^{\text {th}}\) row and the \(j^{\text {th}}\) column. Then the matrix
A*B
consists of elements
\[(\mathrm{AB})_{i, j}=\sum_{k=1}^{n} \mathrm{a}_{i, k} b_{k, j} \nonumber \]
where nn is the number of columns of
A
and the number of rows of
B
. Try typing
A = [1 2 3;4 5 6];B = [7;8;9]; A*B
. You should get the result
ans = 50 112
The inner product between two column vectors
x
and
y
is the scalar defined as the product
x'*y
or equivalently as
y'*x
For example,
x = [1;2],y = [3;4],x'*y
, leads to the result
ans = 11
Similarly, for row vectors the inner product is defined as
x*y'
. The Euclidean norm of a vector is defined as the square root of the inner product between a vector and itself. Try to compute the norm of the vector
[1 2 3 4]
. You should get
5.4772
. The outer product of two column (row) vectors is the matrix
x*y' (x'*y)
.
Any scalar can multiply or be multiplied by a matrix. The multiplication is then performed element by element. Try
A = [1 2 3;4 5 6;7 8 9];A*2
. You should get
ans = 2 4 6 8 10 12 14 26 28
Verify that
2*A
gives the same result.
The inverse of a matrix is computed by using the function
inv
(A)
and is only valid if
A
is square. If the matrix is singular, meaning that it has no inverse, a message will appear. Try typing
inv
(A)
. You should get
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND=2.937385e-18 ans = 1.0e+16* 0.3152 -0.6304 0.3152 -0.6304 1.2609 -0.6304 0.3152 -0.6304 0.3152
The inverse of a matrix may be used to solve a linear system of equations. For example, to solve the system you could type
A = [1 2 3;1 -2 4;0 -2 1]; b = [2;7;3];
inv
(A)*b
and get
ans = 1 -1 1
\[\left[\begin{array}{lll}
1 & 2 & 3 \\
1 & -2 & 4 \\
0 & -2 & 1
\end{array}\right]\left[\begin{array}{l}
x_{1} \\
x_{2} \\
x_{3}
\end{array}\right]=\left[\begin{array}{l}
2 \\
7 \\
3
\end{array}\right] \nonumber \]
Check to see that this is the correct answer by typing
A*[1;-1;1]
. What do you see?
MATLAB offers another way to solve linear systems, based on Gauss elimination, that is faster than computing the inverse. The syntax is
A\b
and is valid whenever
A
has the same number of rows as
b
. Try it.
The “Dot” Operator
Sometimes you may want to perform an operation element by element. In MATLAB, these element-by-element operations are called array operations. Of course, matrix addition and subtraction are already element-by-element operations. The operation
A
.
*B
denotes the multiplication, element by element, of the matrices
A
and
B
. Make two 3×3 matrices,
A
and
B
, and try
≫ A*B ≫ A.*B
Suppose we want to find the square of each number in
A
. The proper way to specify this calculation is
≫ A_squared=A.^2
where the period (dot) indicates an “array operation” to be performed on each element of
A
. Without the dot,
A
is multiplied by
A
according to the rules of matrix multiplication described in Chapter 4, giving a totally different result:
≫ A^2
Subtleties
Because MATLAB can do so many different mathematical functions with just a few keystrokes, there are times when a very slight change in what you type will lead to a different result. Using the matrix
A
entered earlier, type the following two lines:
≫ 2.^A ≫ 2 .^A %with a space after the 2.
In the first case, the dot is “absorbed” by the 2 as a decimal point and the ^ is taken as a matrix exponential. But, when the dot is separated from the 2 by a space, it becomes part of the operator (.^) and specifies that 2 should be raised to the power of each element in A. The point is, you should be very careful to type what you mean in an unambiguous way until you are familiar enough with MATLAB to know how the subtle situations will be interpreted. An unambiguous way of typing the preceding lines is
≫ (2.)^A %for matrix exponential ≫ (2).^A %for array exponential.