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
≫ A = [1 2 3;4 5 6;7 8 9]results in the output
A =
1 2 3
4 5 6
7 8 9The variable A is a matrix of size
≫ x = [-1.3 sqrt(3) (1+2+3)*4/5]results in the matrix
x =
-1.3000 1.7321 4.8000We call a matrix with just one row or one column a vector, and a x(5) = abs(x(1)) to produce the
new vector
x =
-1.3000 1.7321 4.8000 0.000 1.3000Note 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 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 [m n] = size(A). In our
previous example, A = [A;r] is a 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.0000iNote 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 9The 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 3As 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 A in the A*B consists of elements
where 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
112The 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 =
11Similarly, 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 28Verify 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
1Check 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 A and B, and try
≫ A*B
≫ A.*BSuppose we want to find the square of each number in A. The proper
way to specify this calculation is
≫ A_squared=A.^2where 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^2Subtleties. 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
≫ (2.)^A %for matrix exponential
≫ (2).^A %for array exponential.
"Reviewer's Comments: 'I recommend this book as a "required primary textbook." This text attempts to lower the barriers for students that take courses such as Principles of Electrical Engineering, […]"