Matrix operations using MATLAB

Haiyan Tian, The University of Southern Mississippi, Mathematics

Author Profile
Initial Publication Date: October 14, 2024

Summary

A given matrix B transforms a vector x into the vector Bx. If the vector Bx is the input for another transformation defined by matrix A, the resulting vector is then A(Bx). This composite mapping corresponds to the product matrix AB.

The goal of this project is to show that AB can be obtained through multiplying A by each column of B.

Share your modifications and improvements to this activity through the Community Contribution Tool »

Description and Teaching Materials

First, let's create a few special matrices to get familiar with several MATLAB commands.

Display the 2 by 2 identity matrix.

>> eye(2)

ans =

1 0
0 1

Display the 5 by 5 identity matrix.

>> t=5;

>> eye(t)

ans =

1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1

Display the 3 by 2 matrix with diagonal entries being 1 and all other entries being 0.

>> eye(3,2)

ans =

1 0
0 1
0 0

 

Find the dimension of a matrix.

>> size(eye(3))

ans =

3 3

 

Display the 3 by 2 matrix with all entries being 0.

>> zeros(3,2)

ans =

0 0
0 0
0 0

Display the 3 by 2 matrix with all entries being 1.

>> ones(3,2)

ans =

1 1
1 1
1 1

 

Display the 4 by 4 matrix with all entries being random numbers between 0 and 1.

>> rand(4)

ans =

0.4733 0.5497 0.7537 0.0540
0.3517 0.9172 0.3804 0.5308
0.8308 0.2858 0.5678 0.7792
0.5853 0.7572 0.0759 0.9340

 

Display a column vector of 4 entries with each entry being a random number between 0 and 1.

>> rand(4,1)

ans =

0.7577
0.7431
0.3922
0.6555

Generate a 4 by 4 matrix with integer entries, obtained by rounding the entries of rand(4) to the nearest integer towards zero.

>> fix(rand(4))

ans =

0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

 

After generating a 4 by 4 random matrix M=rand(4), we multiply each entry of M by 10 before fixing it. This way we get fewer zero entries.

>> M=rand(4)

M =

0.0838 0.8258 0.4427 0.7749
0.2290 0.5383 0.1067 0.8173
0.9133 0.9961 0.9619 0.8687
0.1524 0.0782 0.0046 0.0844

>> fix(10*M)

ans =

0 8 4 7
2 5 1 8
9 9 9 8
1 0 0 0

Next, let's define a matrix A and vectors x, y, and z. We will calculate Ax, Ay, and Az.

>> A=[5 -4 0 ; -7 1 12 ; 3 2 6 ]

A =

5 -4 0
-7 1 12
3 2 6

 

>> x=[2 ; 5 ; 1]

x =

2
5
1

>> C1=A*x

C1 =

-10
3
22

 

>> y=[2 ; 2; 2]

y =

2
2
2

>> C2=A*y

C2 =

2
12
22

 

>> z=[1 ; 1; 1 ]

z =

1
1
1

>> C3=A*z

C3 =

1
6
11

In the following, let's create a matrix B whose columns are x, y, and z.

We know that x is a column vector whose three entries are as follows. Similarly, we can express the entries of the vectors of y and z.

>> x(1)

ans =

2

>> x(2)

ans =

5

>> x(3)

ans =

1


>> B = [x(1) y(1) z(1) ; x(2) y(2) z(2) ; x(3) y(3) z(3)]

B =

2 2 1
5 2 1
1 2 1

Lastly, we can compute the product AB. Let's denote this product by C, whose columns can be verified the same as C1=Ax, C2=Ay, and C3=Az respectively.

>> C=A*B

C =

-10 2 1
3 12 6
22 22 11

That is, AB = A [x y z] =[Ax Ay Az].