<< Chapter < Page Chapter >> Page >
This module is part of the collection, A First Course in Electrical and Computer Engineering . The LaTeX source files for this collection were created using an optical character recognition technology, and because of this process there may be more errors than usual. Please contact us if you discover any errors.

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

results in the output

The variable A is a matrix of size 3 × 3 . Matrix elements can be any MATLAB expression. For example, the command

results in the matrix

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. Typeto produce the new vector x(5) = abs(x(1))

Note that the size ofhas 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. Dimensions in the command must coincide. Try. r = [13 14],A = [A;r]

The commandgives the number of rows and the number of columns of. The output fromis itself a matrix of size 1 × 2 . These numbers can be stored if necessary by the command. In our previous example,is a 4 × 3 matrix, so the variablewill contain the number 4 andwill contain the number 3. A vector is a matrix for which eitheroris equal to 1. Ifis equal to 1, the matrix is a row vector; ifis equal to 1, the matrix is a column vector. Matrices and vectors may containcomplex numbers. For example, the statement n

and the statement

are equivalent, and they both produce the matrix

Note that blanks must be avoided in the second expression for. Try typing A

What is the size ofnow? A

MATLAB has several built-in functions to manipulate matrices. The special character, ', for prime denotes the transpose of a matrix. The statementproduces the matrix A = [ 1 2 3;4 5 6;7 8 9]'

The rows ofare the column of, and vice versa. Ifis a complex matrix, thenis 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,produces the output x = [1 2 3 4],x = x-1

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,is valid if the second size of(number of columns) is the same as the first size of(number of rows). Let a i , j represent the element ofin the i t h row and the j t h column. Then the matrixconsists of elements A*B

( A B ) i , j = k = 1 n a i , k b k , j

where n is the number of columns ofand the number of rows of. Try typing. You should get the result ; A*B

The inner product between two column vectorsandis the scalar defined as the productor equivalently asFor example,, leads to the result ,x'*y

Similarly, for row vectors the inner product is defined as. The Euclidean norm of a vector is defined as the square root of the inner product betweena vector and itself. Try to compute the norm of the vector. You should get. 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. You should get A = [1 2 3;4 5 6;7 8 9];A*2

Verify thatgives the same result. 2*A

The inverse of a matrix is computed by using the functionand is only valid ifis square. If the matrix is singular, meaning that it has no inverse, a message will appear. Try typing. You should get inv(A)

The inverse of a matrix may be used to solve a linear system of equations. For example, to solve the system

you could typeand get ; inv(A)*b

1 2 3 1 - 2 4 0 - 2 1 x 1 x 2 x 3 = 2 7 3 ,

Check to see that this is the correct answer by typing. What do you see? A*[1;-1;1]

MATLAB offers another way to solve linear systems, based on Gauss elimination, that is faster than computing the inverse. The syntax isand is valid wheneverhas the same number of rows as. Try it. b

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.denotes the multiplication, element by element, of the matricesand. Make two 3 × 3 matrices,and, and try B

Suppose we want to find the square of each number in. The proper way to specify this calculation is A

where the period (dot) indicates an “array operation” to be performed on each element of. Without the dot,is multiplied byaccording to the rules of matrix multiplication described in Chapter 4, giving a totally different result: A

Subtleties. Because MATLAB can do so many different mathematical functions with just a few keystrokes, there are times when a very slight changein what you type will lead to a different result. Using the matrixentered earlier, type the following two lines: A

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 wiIl be interpreted. An unambiguous way of typing the preceding lines is

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, A first course in electrical and computer engineering. OpenStax CNX. Sep 14, 2009 Download for free at http://cnx.org/content/col10685/1.2
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'A first course in electrical and computer engineering' conversation and receive update notifications?

Ask