Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Computer Applications: Matrix Manipulation using MATLAB, Lecture notes of Algebra

This document from Yogyakarta State University's Mathematics and Natural Sciences Faculty, Mathematics Education Study Program, covers the basics of matrix manipulation using MATLAB, including defining matrices, matrix size, transpose, special matrices, building matrices, extracting bits of matrices, dot product, matrix-vector products, matrix-matrix products, and sparse matrices.

What you will learn

  • How do you define matrices in MATLAB?
  • What are dot products and matrix-vector products in MATLAB?
  • How do you perform matrix-matrix products in MATLAB?
  • How do you build and extract bits of matrices in MATLAB?
  • What is the size and transpose of a matrix in MATLAB?
  • What are special matrices in MATLAB?

Typology: Lecture notes

2021/2022

Uploaded on 09/12/2022

techy
techy 🇺🇸

4.8

(9)

262 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ComputerApplications:Topic2Page11
YOGYAKARTA STATE UNIVERSITY
MATHEMATICS AND NATURAL SCIENCES FACULTY
MATHEMATICS EDUCATION STUDY PROGRAM
TOPIC 2
Computer application for manipulating matrix using MATLAB
Definition of Matrices in MATLAB
MATLAB is based on matrix and vector algebra; even scalars are treated as 1x1 matrices.
Therefore, vector and matrix operations are as simple as common calculator operations.
Row and Column vectors are special cases of matrices. An mxn matrix is a rectangular
array of numbers having m rows and n columns. It is usual in a mathematical setting to include
the matrix in either round or square brackets-we shall use square ones.
The best way for you to get started with MATLAB is to learn how to handle matrices. You can
enter matrices into MATLAB in several different ways:
Enter an explicit list of elements.
Load matrices from external data files.
Generate matrices using built-in functions.
Create matrices with your own functions in M-files.
Start by entering a simple matrix as a list of its elements. You only have to follow a few basic
conventions:
Separate the elements of a row with blanks or commas.
Use a semicolon, ; , to indicate the end of each row.
Surround the entire list of elements with square brackets, [ ].
To enter a matrix, simply type in the Command Window
>>A = [1 2 3 ; 4 5 6; 7 8 9]
A=
123
456
789
>> B = [1:5; 6:10; 11:2:20]
B =
1 2 3 4 5
6 7 8 9 10
11 13 15 17 19
So A is 3x3 matrix, and B is 3 x 5. In this context, a row vector is a 1 x n matrix and a column
vector a m x 1 matrix.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Computer Applications: Matrix Manipulation using MATLAB and more Lecture notes Algebra in PDF only on Docsity!

YOGYAKARTA STATE UNIVERSITY

MATHEMATICS AND NATURAL SCIENCES FACULTY

MATHEMATICS EDUCATION STUDY PROGRAM

TOPIC 2

Computer application for manipulating matrix using MATLAB

Definition of Matrices in MATLAB MATLAB is based on matrix and vector algebra; even scalars are treated as 1x1 matrices. Therefore, vector and matrix operations are as simple as common calculator operations. Row and Column vectors are special cases of matrices. An m x n matrix is a rectangular array of numbers having m rows and n columns. It is usual in a mathematical setting to include the matrix in either round or square brackets-we shall use square ones.

The best way for you to get started with MATLAB is to learn how to handle matrices. You can enter matrices into MATLAB in several different ways:

  • Enter an explicit list of elements.
  • Load matrices from external data files.
  • Generate matrices using built-in functions.
  • Create matrices with your own functions in M-files.

Start by entering a simple matrix as a list of its elements. You only have to follow a few basic conventions:

  • Separate the elements of a row with blanks or commas.
  • Use a semicolon, ; , to indicate the end of each row.
  • Surround the entire list of elements with square brackets, [ ].

To enter a matrix, simply type in the Command Window

A = [1 2 3 ; 4 5 6; 7 8 9] A=

>> B = [1:5; 6:10; 11:2:20]

B =

So A is 3x3 matrix, and B is 3 x 5. In this context, a row vector is a 1 x n matrix and a column vector a m x 1 matrix.

Notes that each row in B are generated using equally spaced elements. The middle number defines the increment. If only two numbers are given, then the increment is set to a default of 1.

So 1:5 creates 1 2 3 4 5 and 11:2:20 creates 11 13 15 17 19

Size of a matrix We can get the size (dimensions) of a matrix with the command size

size(A), size(B) ans = 3 3 ans = 3 5 size(ans) ans = 1 2 So A is 3x3 and B is 3x5. The last command size(ans) shows that the value returned by size is itself a 1x2 matrix (a row vector). We can save the results for use in subsequent calculations. [r c] = size(A) r = 3 c = 3 Transpose of a matrix Transposing a vector changes it from a row to a column vector and vice versa. The extension of this idea to matrices is that transposing interchanges rows with the corresponding columns: the 1st row becomes the 1st column, and so on. B,B' B = 1 2 3 4 5 6 7 8 9 10 11 13 15 17 19

ans = 1 6 11 2 7 13 3 8 15 4 9 17 5 10 19

size(B),size(B') ans = 3 5

ans = 5 3

I = eye(3), x = [8; -4; 1], I*x I = 1 0 0 0 1 0 0 0 1

x = 8

  • 1

ans = 8

  • 1

Notice that multiplying the 3 x 1 vector x by the 3 x 3 identity I has no effect (it is like multiplying a number by 1).

Diagonal Matrices A diagonal matrix is similar to the identity matrix except that its diagonal entries are not necessarily equal to 1. To construct the matrix in Matlab, we could either type it in directly

D = [-3 0 0; 0 4 0; 0 0 2] D = -3 0 0 0 4 0 0 0 2 but this becomes impractical when the dimension is large (e.g. a 100 x 100 diagonal matrix). We then use the diag function. We first define a vector d , say, containing the values of the diagonal entries (in order) then diag(d) gives the required matrix. d = [-3 4 2], D = diag(d) d =

-3 4 2

D = -3 0 0 0 4 0 0 0 2

On the other hand, if A is any matrix, the command diag(A) extracts its diagonal entries:

A = [0 1 8 7; 3 -2 -4 2; 4 2 1 1], diag(A) A =

0 1 8 7 3 -2 -4 2 4 2 1 1

ans =

0

  • 1

Notice that the matrix does not have to be square.

Building Matrices It is often convenient to build large matrices from smaller ones:

C=[0 1; 3 -2; 4 2]; x=[8;-4;1]; G = [C x] G = 0 1 8 3 -2 - 4 2 1

C=[-1 2 5; 0 8 6], H=[A;C] C = -1 2 5 0 8 6 H = 1 2 3 4 5 6 7 8 9 -1 2 5 0 8 6

size(B),size(B') ans = 3 5 ans = 5 3 >> I = eye(3), x = [8; -4; 1], I*x I = 1 0 0 0 1 0 0 0 1 x = 8 - 1 ans = 8 - 1 Notice that multiplying the 3 x 1 vector x by the 3 x 3 identity I has no effect (it is like multiplying a number by 1). Diagonal Matrices A diagonal matrix is similar to the identity matrix except that its diagonal entries are not necessarily equal to 1. To construct the matrix in Matlab, we could either type it in directly >> D = [-3 0 0; 0 4 0; 0 0 2] D = -3 0 0 0 4 0 0 0 2 but this becomes impractical when the dimension is large (e.g. a 100 x 100 diagonal matrix). We then use the diag function. We first define a vector d , say, containing the values of the diagonal entries (in order) then diag(d) gives the required matrix. >> d = [-3 4 2], D = diag(d) d = -3 4 2 D = -3 0 0 0 4 0 0 0 2 On the other hand, if A is any matrix, the command diag(A) extracts its diagonal entries: >> A = [0 1 8 7; 3 -2 -4 2; 4 2 1 1], diag(A) A = 0 1 8 7 3 -2 -4 2 4 2 1 1 ans = 0 - 1 Notice that the matrix does not have to be square. Building Matrices It is often convenient to build large matrices from smaller ones: >> C=[0 1; 3 -2; 4 2]; x=[8;-4;1]; >> G = [C x] G = 0 1 8 3 -2 - 4 2 1 >> C=[-1 2 5; 0 8 6], H=[A;C] C = -1 2 5 0 8 6 H = 1 2 3 4 5 6 7 8 9 -1 2 5 0 8 6 so we have added an extra column (x) to C in order to form G and have stacked A and C on top of each other to form H. J = [1:4; 5:8; 9:12; 20 0 5 4] J = 1 2 3 4 5 6 7 8 9 10 11 12 20 0 5 4

K = [ diag(1:4) J; J' zeros(4,4)] K = 1 0 0 0 1 2 3 4 0 2 0 0 5 6 7 8 0 0 3 0 9 10 11 12 0 0 0 4 20 0 5 4 1 5 9 20 0 0 0 0 2 6 10 0 0 0 0 0 3 7 11 5 0 0 0 0 4 8 12 4 0 0 0 0

0 1 2 3 4 5 6 7 8 9

0 1 2 3 4 5 6 7 8 9

nz = 34

ans = 0 2 24 12 -10 - 28 16 9

A.*C ??? Error using ==> times Matrix dimensions must agree.

Matrix-vector products We turn next to the definition of the product of a matrix with a vector. This product is only defined for column vectors that have the same number of entries as the matrix has columns. So, if A is an mxn matrix and x is a column vector of length n, then the matrix-vector Ax is legal. An mxn matrix times an nx1 matrix Æ a m x 1 matrix. We visualize A as being made up of m row vectors stacked on top of each other, then the product corresponds to taking the scalar product of each row of A with the vector x : The result is a column vector with m entries.

It is somewhat easier in Matlab:

A = [5 7 9; 1 -3 -7] A = 5 7 9 1 -3 - x = [8; -4; 1] x = 8

  • 1

A*x ans = 21 13

x*A ??? Error using ==> mtimes Inner matrix dimensions must agree.

Unlike multiplication in arithmetic, Ax is not the same as xA.

Matrix-Matrix Products To form the product of an mxn matrix A and a nxp matrix B , written as AB , we visualize the first matrix ( A ) as being composed of m row vectors of length n stacked on top of each other while the second ( B ) is visualized as being made up of p column vectors of length n :

The entry in the i th row and j th column of the product is then the scalar product of the i th row of A with the j th column of B. The product is an m x p matrix:

Check that you understand what is meant by working out the following examples by hand and comparing with the Matlab answers.

A,B = [0, 1; 3, -2; 4, 2] A = 5 7 9 1 -3 -

B = 0 1 3 - 4 2

C = A*B C = 57 9 -37 -

D = BA D = 1 -3 - 13 27 41 22 22 22 E = B'A' E = 57 - 9 - We see that E = C' suggesting that (AB)' = B'A' Why is B x A a 3 x 3 matrix while A x B is 2 x 2?

Sparse Matrices Matlab has powerful techniques for handling sparse matrices these are generally large matrices (to make the extra work involved worthwhile) that have only a very small proportion of non{zero entries. Example: Create a sparse 5 x 4 matrix S having only 3 non-zero values: S1,2 = 10, S3,3 = 11 and S5,4 =12.

B = spdiags([k m u],-1:1,n,n) B = (1,1) 1 (2,1) - (1,2) 5 (2,2) 2 (3,2) - (2,3) 4 (3,3) 3 (4,3) - (3,4) 3 (4,4) 4 (5,4) - (4,5) 2 (5,5) 5 full(B) ans = 1 5 0 0 0

-2 2 4 0 0 0 -3 3 3 0 0 0 -4 4 2 0 0 0 -5 5 Inverse of Matrix MATLAB function inv is used to compute the inverse matrix. Let the matrix A be defined as follows A = [1 2 3;4 5 6;7 8 10] A = 1 2 3 4 5 6 7 8 10 Then B = inv(A) B = -0.6667 -1.3333 1. -0.6667 3.6667 -2. 1.0000 -2.0000 1.

Determinant In some applications of linear algebra knowledge of the determinant of a matrix is required. MATLAB built-in function det is designed for computing determinants. Let A = magic(3); Determinant of A is equal to det(A) ans =

The sum Function The “sum" applied to a vector adds up its components (as in sum(1:10)) while, for a matrix, it adds up the components in each column and returns a row vector. sum(sum(A)) then sums all the entries of A.

A = [1:3; 4:6; 7:9] A = 1 2 3 4 5 6 7 8 9 s = sum(A), ss = sum(sum(A)) s = 12 15 18

ss =

45 max & min These functions act in a similar way to sum. If x is a vector, then max(x) returns the largest element in x

x = [1.3 -2.4 0 2.3], max(x), max(abs(x)) x = 1.3000 -2.4000 0 2. ans =

ans =

[m, j] = max(x) m =

j = 4 When we ask for two outputs, the first gives us the maximum entry and the second the index of the maximum element. For a matrix, A, max(A) returns a row vector containing the maximum element from each column. Thus to find the largest element in A we have to use max(max(A)).

find for matrices The function “find" returns a list of the positions (indices) of the elements of a matrices satisfying a given condition. For example,

A = [ -2 3 4 4; 0 5 -1 6; 6 8 0 1] A = -2 3 4 4 0 5 -1 6 6 8 0 1 k = find(A==0) k = 2 9