1.2 Matrix Operations

Objectives

To learn methods for entering and manipulating matrices via MATLAB.

Prerequisites

Fundamentals of matrix algebra.

Background

The matrix (rectangular array of numbers) is the fundamental data object of MATLAB. Matrices are entered as rows of numbers enclosed in square brackets. Each row is entered on a separate line or separated by semicolons. Each number in a row is separated by one or more spaces, or by a comma. For example:

» A = [1 2 3; 4 5 6; 7 8 9]                Enter matrix A.

A =

1 2 3

4 5 6

7 8 9

» B = [1, 2; 3, 4]                                Enter matrix B.

B =

1 2

3 4

» C = B.^3                                  C has elements of B raised to 3rd power.

C =

1 8

27 64

» C = C.^3                                    Each element of C is raised to 3rd power.

C =

1 512

19683 262144

» D = [1 0 0; 2 3 1];                    Enter matrix D. Semicolon at end suppresses output.

» D*A                                            Compute matrix product DA.

ans =

1 2 3

21 27 33

As you can see from the sample session, expressions representing operations on matrices can be evaluated and assigned to other matrices. Listed in the table below are matrix operations and their symbols in MATLAB, as well as some of the most often-used matrix functions.

Summary of Matrix Operations

Operation                 MATLAB         Symbol Example

Addition                         +                     A + B

Subtraction                     -                     A - B

Multiplication               *                     A * B

Transpose                     '                         A'

Matrix Power                ^                     A^3

Scalar Multiplication     *                     4 * A

Element by Element (Array) Operations

Multiplication                 .*                 A .* B

Division                         ./                    A ./ B

Power                         .^                     A.^2

Matrix Building Operators

Select submatrix             :                     A(:,1)        A(1:2,3:5)

Adjoin matrices             [ ]                 [A B]

Generate vectors             :                 X=0:1:10

Special Matrices

nxn Identity Matrix     eye(n)             eye(3)

nxm Zero Matrix         zeros(n,m)     zero(3,4)

nxm Matrix of all 1's     ones(n,m)     ones(5,2)

nxm Random Matrix     rand(n,m)     rand(4,5)    ( random entries between 0 and 1)


In the laboratory that follows, you will practice entering and manipulating matrices, using some of the above operations as well as others introduced in the context of the laboratory exercises.


Laboratory on Matrix Operations

Start up MATLAB and, if your instructor requests it, create a diary of your session.

1. Enter the necessary statements to create the following matrices in MATLAB:

A = B = C = D = 

2. Compute the following using MATLAB. Write down the result returned by MATLAB . If you receive an error message instead, explain the error.
 

a) A + B

b) A - 2*B

c) A^3

d) B'

e) eye(3)

f) A - 2*eye(3)

g) C*D

h) D*C

i) A*B

j) A.*B
 

3. Your most recent result is always stored in the variable ans.

a) Enter ans to see your most recent result

b) To determine what the existing variables are, type in the command who.

c) The number of floating point operations required by MATLAB to carry out your computations is stored in the variable flops. Type in flops to see how many "flops" you have used, and write down the information printed by MATLAB.

d) Now type

>> flops(0) Note: This resets the flop count to 0

>> A*B

>> flops

to see the number of floating point operations needed to compute the product AB. What number is returned?  Calculate the total number of multiplications and additions to compute the product AB.

Does this correspond to the number of flops reported?

4. Manipulating matrices

Enter the following commands, and then explain what they do.

a) A(1,:)
b) A(:,1)
c) A(2,3)
d) A(1:3,1:2)
e) D = [A B]
f) E = A(1,:)
g) A(2,:) = [ 1 0 0 0 0 0]
h) A([1 2],:) = A([2 1],:)
i) X = 0:1:10
j) X = 1:0.1:2

5. Creating Random Matrices

A = rand(n,m) generates a nxm matrix with real entries between 0 and 1. By applying matrix operations and built-in functions, you can easily generate random matrices with desired properties and ranges of values. For example, A = 2*rand(n,m) -1 generates a nxm matrix with real entries between -1 and 1. A = round(10*(2*rand(n,m)-1)) generates an nxm matrix with integer entries between 10 and -10. Write down the command to create each of the following random matrices and enter that command, verifying your answer.

a) a random 3x3 matrix with real entries between 0 and 1
b) a random 4x3 matrix with real entries between -1 and 1
c) a random 2x4 matrix with integer entries between -5 and 5
d) a random 4x4 matrix with integer entries which are either 0 or 1.

6. Enter the following and then write down the result of each statement:

a) V = [1 3 1]
b) A = 2 * eye(3)
c) B = diag(V,0)
d) C = A - B - B'
e) sqrt(A)

If you have a diary file open recording your current session, remember to enter diary off when done!