Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 3.

Slides:



Advertisements
Similar presentations
GG313 Lecture 12 Matrix Operations Sept 29, 2005.
Advertisements

EGR 106 – Week 4 – Math on Arrays
Part 3 Chapter 8 Linear Algebraic Equations and Matrices PowerPoints organized by Dr. Michael R. Gustafson II, Duke University All images copyright © The.
Matrix Mathematics in MATLAB and Excel
Linear regression models in matrix terms. The regression function in matrix terms.
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
CE 311 K - Introduction to Computer Methods Daene C. McKinney
Modified Gary Larson Far Side cartoon
1 Chapter 3 Matrix Algebra with MATLAB Basic matrix definitions and operations were covered in Chapter 2. We will now consider how these operations are.
Chapter 7 Matrix Mathematics Matrix Operations Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Row 1 Row 2 Row 3 Row m Column 1Column 2Column 3 Column 4.
Chapter 10 Review: Matrix Algebra
Array Math.
THU, JAN 8, 2015 Create a “Big Book of Matrices” flip book using 4 pages. Do not make your tabs big! BIG BOOK OF MATRICES What is a Matrix? Adding & Subtracting.
1 Week 3: Vectors and Matrices (Part III) READING: 2.2 – 2.4 EECS Introduction to Computing for the Physical Sciences.
CSE123 Lecture 5 Arrays and Array Operations. Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars,
Array Addition  Two arrays can be added if and only if both arrays have exactly the same dimensions.  Assuming the dimension requirement is satisfied,
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Learner’s Guide to MATLAB® Chapter 2 : Working with Arrays.
Statistics and Linear Algebra (the real thing). Vector A vector is a rectangular arrangement of number in several rows and one column. A vector is denoted.
Fin500J Mathematical Foundations in Finance Topic 1: Matrix Algebra Philip H. Dybvig Reference: Mathematics for Economists, Carl Simon and Lawrence Blume,
CMPS 1371 Introduction to Computing for Engineers MATRICES.
Matrices Addition & Subtraction Scalar Multiplication & Multiplication Determinants Inverses Solving Systems – 2x2 & 3x3 Cramer’s Rule.
Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Lecture 28: Mathematical Insight and Engineering.
Unit 3: Matrices.
Matrices A matrix is a table or array of numbers arranged in rows and columns The order of a matrix is given by stating its dimensions. This is known as.
Recap Script M-file Editor/Debugger Window Cell Mode Chapter 3 “Built in MATLAB Function” Using Built-in Functions Using the HELP Feature Window HELP.
A string is an array of characters Strings have many uses in MATLAB Display text output Specify formatting for plots Input arguments for some functions.
Prepared by Deluar Jahan Moloy Lecturer Northern University Bangladesh
ES 240: Scientific and Engineering Computation. Chapter 8 Chapter 8: Linear Algebraic Equations and Matrices Uchechukwu Ofoegbu Temple University.
Meeting 18 Matrix Operations. Matrix If A is an m x n matrix - that is, a matrix with m rows and n columns – then the scalar entry in the i th row and.
Slide Copyright © 2009 Pearson Education, Inc. 7.3 Matrices.
Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
Array Operations ENGR 1181 MATLAB 4.
Matrices: Simplifying Algebraic Expressions Combining Like Terms & Distributive Property.
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Matrices and Matrix Operations. Matrices An m×n matrix A is a rectangular array of mn real numbers arranged in m horizontal rows and n vertical columns.
MATRIX A set of numbers arranged in rows and columns enclosed in round or square brackets is called a matrix. The order of a matrix gives the number of.
Finishing up Chapter 5. Will this code enter the if statement? G=[30,55,10] if G
ENG College of Engineering Engineering Education Innovation Center 1 Array Operations in MATLAB 1.Types of Matrix arithmetic 2.Dot operators Mathematical.
Unit 3: Matrices. Matrix: A rectangular arrangement of data into rows and columns, identified by capital letters. Matrix Dimensions: Number of rows, m,
Matrices. Matrix - a rectangular array of variables or constants in horizontal rows and vertical columns enclosed in brackets. Element - each value in.
If A and B are both m × n matrices then the sum of A and B, denoted A + B, is a matrix obtained by adding corresponding elements of A and B. add these.
Matrices. Variety of engineering problems lead to the need to solve systems of linear equations matrixcolumn vectors.
MTH108 Business Math I Lecture 20.
Linear Algebraic Equations and Matrices
12-1 Organizing Data Using Matrices
Chapter 7 Matrix Mathematics
Matrix Operations.
Matrix Operations SpringSemester 2017.
EGR 106 – Week 4 – Math on Arrays
Section 7.4 Matrix Algebra.
Matrix Algebra.
MATRICES MATRIX OPERATIONS.
MATRICES MATRIX OPERATIONS.
Unit 3: Matrices
2.2 Introduction to Matrices
Matrix Algebra.
3.5 Perform Basic Matrix Operations
Chapter 4 Matrices & Determinants
1.8 Matrices.
MATRICES MATRIX OPERATIONS.
MATRICES MATRIX OPERATIONS.
Matrix Operations Ms. Olifer.
Matrix Operations SpringSemester 2017.
1.8 Matrices.
3.5 Perform Basic Matrix Operations Algebra II.
Presentation transcript:

Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 3 Mathematical Operations with Arrays

3.0 Previously dealt with scalars (single numbers). Will now work with arrays, which in general have more than one number This chapter covers basics of using arrays 2

3.0 array – a rectangular arrangement of numbers with one or more dimensions vector – an array with only one column or one row scalar – an array with exactly one row and one column, i.e., a single number Note – "matrix" and "array" are often used synonymously 3

3.1 Addition and Subtraction ▪ Use + to add two arrays or to add a scalar to an array ▪ Use – to subtract one array from another or to subtract a scalar from an array ▪ When using two arrays, they must both have the same dimensions (number of rows and number of columns) ▪ Vectors must have the same dimensions (rows and columns), not just the same number of elements 4

3.1 Addition and Subtraction When adding two arrays A and B, MATLAB adds the corresponding elements, i.e., ▪ It adds the element in the first row and first column of A to the element in the first row and column of B ▪ It adds the element in the first row and second column of A to the element in the first row and second column of B, etc. This called elementwise addition 5

3.1 Addition and Subtraction When subtracting two arrays A and B, MATLAB performs an elementwise subtraction In general, an operation between two arrays that works on corresponding elements is called an elementwise operation 6

3.1 Addition and Subtraction 7

When adding a scalar to an array, MATLAB adds the scalar to every element of the array When subtracting a scalar from an array, MATLAB subtracts the scalar from every element of the array 8

3.1 Addition and Subtraction 9

3.2 Array Multiplication There are two ways of multiplying matrices – matrix multiplication and elementwise multiplication MATRIX MULTIPLICATION ▪ Type used in linear algebra ▪ MATLAB denotes this with asterisk ( * ) ▪ Number of columns in left matrix must be same as number of rows in right matrix 10

3.2 Array Multiplication 11

3.2 Array Multiplication 12

3.2 Array Multiplication 13

3.2 Array Multiplication >> A = randi(3,3) A = >> B=randi(3,3) B = >> AB = A*B AB = >> BA = B*A BA = >> AB == BA ans =

3.2 Array Multiplication When performing matrix multiplication on two vectors ▪ They must both be the same size ▪ One must be a row vector and the other a column vector ▪ If the row vector is on the left, the product is a scalar ▪ If the row vector is on the right, the product is a square matrix whose side is the same size as the vectors 15

3.2 Array Multiplication >> h = [ ] h = >> v = [ ]' v = 0 1 >> h * v ans = 4 >> v * h ans =

3.2 Array Multiplication dot(a,b) computes inner (dot) product ▪ a and b must be same size ▪ Any combination of vertical or horizontal vectors ▪ Result is always a scalar EXAMPLE >> h = [ ] h = >> v = [ ]' v = 0 1 >> dot(h,v) ans = 4 >> dot(v,h) ans = 4 17

3.3 Array Division Identity matrix ▪ Square matrix with ones on main diagonal and zeros elsewhere ▪ When do matrix multiplication on any array or vector with the identity matrix, array or vector is unchanged ▪ True whether multiply with identity matrix on left or on right ▪ MATLAB command eye(n) makes an n×n identity matrix 18

3.3 Array Division 19

3.3 Array Division In math, inverse of a matrix A is written as A -1 In MATLAB, get inverse with A^-1 or inv(A) 20

3.3 Array Division Determinants: A determinant is a function associated with square matrices ▪ In math, determinant of A is written as det(A) or |A| ▪ In MATLAB, compute determinant of A with det(A) ▪ A matrix has an inverse only if it is square and its determinant is not zero If you don't remember much about determinants, go back to your linear algebra book and review them 21

3.3 Array Division Left division, \ : Left division is one of MATLAB's two kinds of array division ▪ Used to solve the matrix equation AX=B ▪ A is a square matrix, X, B are column vectors ▪ Solution is X = A -1 B In MATLAB, solve by using left division operator (\), i.e., >> X = A \ B 22

3.3 Array Division When solving set of linear equations, use left division, not inverse, i.e., use X=A\B not X=inv(A)*B Left division is ▪ 2-3 times faster ▪ Often produces smaller error than inv() ▪ Sometimes inv() can produce erroneous results 23

3.3 Array Division Right division, /: Right division is the other kind of MATLAB's array division ▪ Used to solve the matrix equation XC=D ▪ C is a square matrix, X, D are row vectors ▪ Solution is X = D·C -1 In MATLAB, solve by using right division operator (/), i.e., >> X = D / C 24

3.4 Element-by-Element Operations Another way of saying elementwise operations is element-by-element operations ▪ Addition and subtraction of arrays is always elementwise ▪ Multiplication, division, exponentiation of arrays can be elementwise ▪ Both arrays must be same dimension 25

3.4 Element-by-Element Operations Do elementwise multiplication, division, exponentiation by putting a period in front of the arithmetic operator 26

3.4 Element-by-Element Operations 27

3.4 Element-by-Element Operations 28

3.4 Element-by-Element Operations ELEMENTWISE MULTIPLICATION ▪ Use.* to get elementwise multiplication (notice period before asterisk) ▪ Both matrices must have the same dimensions >> A = [1 2; 3 4]; >> B = [0 1/2; 1 -1/2]; >> C = A.* B >> C =

3.4 Element-by-Element Operations If matrices not same dimension in elementwise multiplication, MATLAB gives error >> A = [ 1 2; 3 4]; >> B = [1 0]'; >> A.* B % Meant matrix multiplication! ??? Error using ==> times Matrix dimensions must agree. >> A * B % this works ans =

3.4 Element-by-Element Operations Be careful – when multiplying square matrices ▪ Both types of multiplication always work ▪ If you specify the wrong operator, MATLAB will do the wrong computation and there will be no error! ▪ Difficult to find this kind of mistake 31

3.4 Element-by-Element Operations EXAMPLE >> A = [1 2; 3 4]; >> B = [0 1/2; 1 -1/2]; >> A.* B >> ans >> A * B ans =

3.4 Element-by-Element Operations Elementwise computations useful for calculating value of a function at many values of its argument 33

3.5 Using Arrays in MATLAB Built-in Functions Built-in MATLAB functions can accept arrays as inputs ▪ When input is array, output is array of same size with each element being result of function applied to corresponding input element ▪ Example: if x is a 7-element row vector, cos(x) is [cos(x 1 ) cos(x 2 ) cos(x 3 ) cos(x 4 ) cos(x 5 ) cos(x 6 ) cos(x 7 )] 34

3.6 Built-in Functions for Analyzing Arrays MATLAB has lots of functions for operating on arrays. For a vector v ▪ mean(v) – mean (average) ▪ max(v) – maximum value, optionally with index of maximum ▪ min(v) – minimum value, optionally with index of minimum ▪ sum(v) – sum ▪ sort(v) – elements sorted into ascending order 35

3.6 Built-in Functions for Analyzing Arrays ▪ median(v) – median ▪ std(v) – standard deviation ▪ dot(v,w) – dot (inner product); v, w both vectors of same size but any dimension ▪ cross(v,w) – cross product; v, w must both have three elements but any dimension ▪ det(A) – determinant of square matrix A ▪ inv(A) – inverse of square matrix A 36

3.6 Built-in Functions for Analyzing Arrays See Table 3-1 for details on the preceding functions Note that in all but last four functions of Table 3-1, A is a vector, not a matrix. The table does not apply if A is a matrix 37

3.7 Generation of Random Numbers Random numbers often used in MATLAB engineering applications ▪ Simulate noise ▪ Useful in certain mathematical computations, such as Monte Carlo simulations 38

3.7 Generation of Random Numbers MATLAB has three commands that create random numbers – rand, randn, randi ▪ All can create scalars, vectors, or matrices of random numbers 39

3.7 Generation of Random Numbers rand generates random numbers uniformly distributed between 0 and 1 ▪ To get numbers between a and b, multiply output of rand by b-a and add a, i.e., (b-a)*rand + a See Table 3-2 for some of different ways of calling rand 40

3.7 Generation of Random Numbers randi generates uniformly distributed random integers in a specified range For example, to make a 3×4 of random numbers between 50 and 90 >> d=randi( [50 90],3,4) d = See Table 3-3 for some of different ways of calling randi 41

3.7 Generation of Random Numbers randn generates random numbers from a normal distribution with mean 0 and standard deviation 1 Can call randn in same ways as rand, as Table 3-2 shows 42

3.7 Generation of Random Numbers To get numbers with mean μ and standard deviation σ, multiply output of randn by μ and add σ, i.e., sigma * rand + mu 43

3.7 Generation of Random Numbers To get normally distributed integers apply the round function to previous formula, i.e., round( sigma * rand + mu ) EXAMPLE >> w = round(4*randn(1,6)+50) W =