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

MATLAB Workshop 12: Understanding Matrices (Arrays) and Their Operations, Study notes of Algebra

A workshop guide for using matrices (arrays) in MATLAB. It covers creating matrices, matrix properties, transposing arrays, extracting rows and columns, scalar-matrix operations, and matrix algebra. The document also includes exercises for practicing these concepts.

Typology: Study notes

2021/2022

Uploaded on 09/12/2022

alpana
alpana 🇺🇸

4.9

(13)

249 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
MATLAB: Workshop 12 - Matrices (Arrays) page 1
MATLAB Workshop 12 - Matrices (Arrays)
Objectives: Learn about matrix properties in MATLAB, methods to create matrices, mathematical
functions with matrices, element-by-element matrix operations, and matrix algebra.
MATLAB Features:
vector/matrix variable properties
Property Comment
var_name
user chooses names
name should represent meaning of variable
ex: use radius rather than x or s for radius
letters, digits (0-9), underscore ( _ ) allowed in name
must start with a letter
case sensitive ( Radius is different than radius )
can be any length, but first 31 characters must be unique
value
all numerical values are
double precision
~ 16 significant figures (not decimal places)
and imaginary
has both real and imaginary parts
a±
±±
±bi (where i=( 1))
both a and b are double precision
if b=0, MATLAB only displays real part, a
memory
location MATLAB assigns - you do not need to worry about this
order of precedence rules for vector/matrix arithmetic operators
Order of
Precedence Symbol Meaning
1 () group together
'transpose (exchange rows and columns)
.^ raise each element to indicated power
2 ^multiply matrix by itself indicated number of times
.* element-by-element multiplication
./ element-by-element right division
.\ element-by-element left division
*multiply two matrices
/matrix right division
3
\matrix left division
+addition
4 -subtraction
colon (:) operator - a special vector/matrix operator
Notation Action Result
a:c creates vector with elements 1 apart [a a+1 a+2 ... c]
a:b:c creates vector with elements b apart [a a+b a+2b ... (
c)]
A(m,:) selects mth row of A
A(:,j) selects jth column of A
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download MATLAB Workshop 12: Understanding Matrices (Arrays) and Their Operations and more Study notes Algebra in PDF only on Docsity!

MATLAB Workshop 12 - Matrices (Arrays)

Objectives : Learn about matrix properties in MATLAB, methods to create matrices, mathematical

functions with matrices, element-by-element matrix operations, and matrix algebra.

MATLAB Features :

vector/matrix variable properties

Property Comment

var_name

user chooses names

  • name should represent meaning of variable ex: use radius rather than x or s for radius
  • letters, digits (0-9), underscore ( _ ) allowed in name
  • must start with a letter
  • case sensitive ( Radius is different than radius )
  • can be any length, but first 31 characters must be unique

value

all numerical values are

  • double precision ~ 16 significant figures (not decimal places)
  • and imaginary has both real and imaginary parts

a ±±±± bi (where i = ( − 1 ))

both a and b are double precision if b = 0, MATLAB only displays real part, a memory location MATLAB assigns - you do not need to worry about this

order of precedence rules for vector/matrix arithmetic operators

Order of Precedence Symbol Meaning

1 ( )^ group together ' (^) transpose (exchange rows and columns) 2 .^ (^) raise each element to indicated power ^ (^) multiply matrix by itself indicated number of times .* (^) element-by-element multiplication ./ (^) element-by-element right division .\ (^) element-by-element left division

  • (^) multiply two matrices / (^) matrix right division

\ (^) matrix left division

  • (^) addition 4
  • (^) subtraction

colon ( : ) operator - a special vector/matrix operator

Notation Action Result a:c (^) creates vector with elements 1 apart [a a+1 a+2 ... c] a:b:c creates vector with elements b apart (^) [a a+b a+2b ... (≤≤≤≤c)] A(m,:) (^) selects m th^ row of A A(:,j) selects j th^ column of A

A(m:n,:) selects m th^ through nth^ rows of A A(:,j:k) selects j th^ through k th^ columns of A

A(m:n,j:k) selects m th^ through nth^ rows of A and j th^ through k th^ columns of A

matrix functions

number of rows and columns

[rows, cols] = size(x)

» Array2trans = Array2' Array2trans = 3 -4 12 12 -3 5

Array2 has been “transposed”, i.e., the columns and rows were interchanged so that the first column became the first row, etc.

(3) Extracting rows or columns from an array in MATLAB.

Enter the following at the Command Line prompt » row2_Array1 = Array1(2,:) row2_Array1 = 4 -3 6 The second row of Array1 has been extracted using the colon operator.

Enter the following at the Command Line prompt » col1_Array2 = Array2(:,1) col1_Array2 = 3

12 The first column of Array2 has been extracted using the colon operator.

Enter the following at the Command Line prompt » Array1sub = Array1(1:2,2:3) Array1sub = 9 - -3 6 A subset array consisting of elements from the 1st^ and 2nd^ rows, 2nd^ and 3rd^ columns of Array1 has been created.

(4) Determining the size of a matrix.

Enter the following at the Command Line prompt » size(Array1) ans = 3 3 size returns the number rows and columns in an array.

Enter the following at the Command Line prompt » [A2rows, A2cols] = size(Array2) A2rows = 3 A2cols = 2 We can assign the number of rows and columns in a matrix to variables for later use.

Enter the following at the Command Line prompt

  • The colon operator is a powerful tool for extracting parts of an array for further use.

» size(row2_Array1) ans = 1 3 The size of a row vector is (1,N) for 1 row, and N elements. What do you think the size of a column vector is? Contrast size with length for a vector.

  • Function and element-by-element operations with matrices in MATLAB

MATLAB processes mathematical functions and element-by-element operations for matrices in

the same manner as it does for vectors. That is, application of a function, e.g.

B = exp(A)

where A is an array will raise every element in A to the eth^ power to produce the corresponding elements

of array B. Likewise, the element-by-element operators, .^, .*, ./, and ., are used to process

matrices element-by-element.

(5) Functions of matrices in MATLAB.

Enter the following at the Command Line prompt » Amat = [1 10; 100 1000] Amat = 1 10 100 1000 » log10_Amat = log10(Amat) log10_Amat = 0 1. 2.0000 3. » » Bmat = [0 pi/6 pi/3; pi/2 2pi/3 5pi/6] Bmat = 0 0.5236 1. 1.5708 2.0944 2. » sin_Bmat = sin(Bmat) sin_Bmat = 0 0.5000 0. 1.0000 0.8660 0.

(6) Scalar-matrix operations in MATLAB.

Enter the following at the Command Line prompt » const = 5; » Cmat = Amat + const Cmat = 6 15 105 1005 The constant value 5 was added to each element of Cmat.

Enter the following at the Command Line prompt

  • The size function returns the number of rows and columns in a matrix.
  • Mathematical functions operate on a matrix element-by-element.

Cmat are displayed as 1.0e+003 *. This means that each number that follows should be multiplied by 10 3 in order to produce the proper element value. MATLAB automatically moves to scientific notation type display when necessary.

Contrast the preceding with » Hmat = Amat*log10_Amat Hmat = 1.0e+003 * 0.0200 0. 2.0000 3. Using the multiplication operator, , instead of the element-by-element multiplication operator, ., instructed MATLAB to follow the rules of matrix multiplication (outlined below) and produces an entirely different answer.

  • Matrix algebra

Matrices are characterized by their dimension. For simplicity, [ A ] MxN will denote a matrix with

M rows and N columns. Likewise, ai,j will denote the element value in the i

th

row, j

th

column of

[ A ] MxN. The row dimension will always come first and the column dimension second. The rules for

basic matrix algebra operations are as follows.

Matrix addition/subtraction. Two matrices can be added or subtracted if and only if they have the

same dimensions. The result is a matrix with the same dimensions. Thus,

[ C ] 3 x 2 =[ A ] 3 x 2 +[ B ] 3 x 2

is possible while

[ A ] 3 x 2 +[ D ] 2 x 3

is not. Addition of two matrices is done element-by-element as

c i , j = ai , j + bi , j

for each element in the matrices. Matrix subtraction is defined as

d i , j = ai , j − bi , j

Note that matrix addition is commutative and matrix subtraction is not commutative.

Matrix multiplication. Two matrices can be multiplied if and only if the number of columns in

the first matrix is the same as the number of rows in the second matrix. The result is a matrix with the

same number of rows as the first matrix and the same number of columns as the second matrix. Thus

[ C ] PxS =[ A ] PxQ [ B ] QxS

is possible while

[ A ] PxQ [ B ] PxQ

is not. Multiplication of two matrices is defined as

å

=

k Q

c i , j k 1 ( ai , k )( bk , j )

for each element in the resulting matrix. Note that, in general, matrix multiplication is not

commutative.

(8) Matrix algebra in MATLAB.

  • Confusing matrix algebra operators with element-by-element operators can result in big mistakes.

Define (enter) the following matrices at the MATLAB command prompt.

ú

ú

ú

û

ù

ê

ê

ê

ë

é

Amat

ú

ú

ú

û

ù

ê

ê

ê

ë

é

Bmat

ú

ú

ú

û

ù

ê

ê

ê

ë

é

Cmat

Try performing the following mathematical operations

Amat + Bmat Amat + Cmat Bmat − Amat Cmat − Bmat

Amat * Bmat Amat * Cmat Bmat * Amat Cmat * Amat

What are the results? What error messages are generated?

Exercises : Perform the following operations using array arithmetic where appropriate.

1. Create the following three matrices:

ú

ú

ú

û

ù

ê

ê

ê

ë

é

A

ú

ú

ú

û

ù

ê

ê

ê

ë

é

B

ú

ú

ú

û

ù

ê

ê

ê

ë

é

C

a. Show that matrix addition is commutative by computing A+B and B+A.

b. Show that matrix addition is associative by computing A+(B+C) and (A+B)+C.

c. Show that scalar-matrix multiplication is distributive by computing 5(A+C) and 5A+5C.

d. Show that matrix multiplication is distributive by computing A(B+C) and AB+B*C.

2. Use the matrices from exercise 1 to answer the following

a. Does AB = BA?

b. Does (AB)C = A(BC)?

c. Does (A*B)t^ = At^ *Bt^ (t means transpose)?

d. Does (A+B)

t

= A

t

+B

t

e. Does AB = A.B?

Recap : You should have learned

  • How to declare row and column vectors in MATLAB
  • How to declare a vector with evenly spaced elements (two methods)