4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices?...

36

Transcript of 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices?...

Page 1: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.
Page 2: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-2

How can you create matrices ?

How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices.

Use the built-in functions eye , ones, zeros and diag to create matrices.

Learn about the size function the matrix counterpart to the length vector function

Readings: Matlab by Pratap Chapters 2.3.1, 2.3.2, 5.1

Page 3: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

3

In Matlab (for CS101) all data will be stored in arrays. An array can represent a: vector - size 1 x n (row vector with 1 row and n columns) or

n x 1 (column vector with n rows and 1 column) matrix - any size m x n (m rows and n columns).

Conventions Two arrays are defined to be equal if they are of the same size and at each position, they have the same value.

Page 4: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-4

A matrix is like a table with rows (horizontal) and columns (vertical). Specific elements are referenced by the pair (row,column).

Example: For a general 2 x 2 matrix the math notation is,

Example: In Matlab the above matrix is created by

And the sub-scripted variablestake values such as:

m11 = 2m12 = 3m21 = 3m22 = -2

>> M = [2 3 ; 3 -2]; Note the use of the “ ; “ operator.

2221

1211

mm

mmM

Page 5: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-5

>> M1 = [2 3 ; 4 5] M1 = 2 3 4 5>> M2 = M1’ M2 = 2 4 3 5

The transpose operator ‘ makes the first row of M1 become the first column of M2 and the second row of M1 become the second column of M2.

Page 6: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-6

Arrays can be constructed from built-in Matlab functions (see Pratap pp. 70-71). >> A = eye(3) (eye(n) constructs an n x n “identity” matrix)A =

1 0 00 1 00 0 1

>> A = ones(3,2) (ones(m,n) constructs an m x n array of 1’s)A =

1 11 11 1

>> A = zeros(2,3) (zeros(m,n) constructs an m x n array of 0’s)A =

0 0 00 0 0

Page 7: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-7

Arrays can be constructed from the diag function and a given vector (see 2.9) >> K = [1 2 3];>> diag(K,0) (diag(K,0) constructs a diagonal matrix with values of K down the diagonal)

1 0 00 2 0 (green line represents the “diagonal” of a matrix”) 0 0 3

K = [2 3];>> diag(K,1)(diag(K,1) constructs a matrix with values of K above the diagonal)

0 2 00 0 30 0 0

Page 8: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-8

>> K = [3 4];>> diag(K,-1) (diag(K,-1) constructs a diagonal matrix with values of K below the diagonal)

0 0 03 0 00 4 0

Page 9: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-9

Arrays can be replicated and tiled to make larger matricies.. Example:

>> A = [1 2 ; 3 4] A = 1 2 3 4Create a matrix named B that tiles A

in the following manner

>> B = repmat(A,3,4) (constructs an 3 x 4 tiling of A)B =

1 2 1 2 1 2 1 2 3 4 3 4 3 4 3 4 1 2 1 2 1 2 1 2 3 4 3 4 3 4 3 4 1 2 1 2 1 2 1 2 3 4 3 4 3 4 3 4

A A A A

A A A A

A A A A

Page 10: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-10

>> mat1 = [1 -2 ;5 3 ] ;Example

>> mat1(1,1) ( first row ,first column)

ans = 1

>> mat1(1,2) ( first row ,second column)

ans = -2

>> mat1(2,1) (second row, first column)

ans = 5

>> mat1(2,2) (second row, second column)

ans = 3

Page 11: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-11

You can select an entire row or column of a matrix by using the colon “ : ” symbol.

>> mat1 = [1 -2 3 -4 ; 5 3 7 8 ; 9 10 11 12] mat1 = 1 -2 3 -4 5 3 7 8 9 10 11 12

>> mat1( 1 , : ) % (first row, any column)ans =

1 -2 3 -4

>> mat1( : , 2) % (any row , second column)ans =

-2 3

10

Page 12: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-12

>> mat1 = [1 -2 3 -4 ; 5 3 7 8 ; 9 10 11 12] mat1 = 1 -2 3 -4 5 3 7 8 9 10 11 12

>> mat1( end , end ) % (third row, fourth column)ans =

12

>> mat1( [1 2] , [1 4] ) % (first and second row, first and fourth column)ans =

1 -4 5 8

Page 13: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-13

You can use subscripting on a Matlab variable to modifythe value of the matrix. Note that the matrix is locatedon the left hand side of the = .

>> mat1 = [1 -2 ; 5 3 ] ;>> mat1(1,:) = 4 mat =

4 4 5 3 >> mat1(2, 2) = 7mat1 =

4 4 5 7>> mat1(: ,1) = [ -1 -2] (or [-1 -2]’) mat1 =

-1 4-2 7

Page 14: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-14

(dot form)

Rules for the valid operations. If A and B are arrays then it is not always true that >>A op B is defined when op is one of the operations in the table above.The size of an array is its number of rows and columns.

We will consider some operators on a case by case basis.

add sub mult right div left div power

+ - * / \ ^

.* ./ .\ .^

Page 15: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-15

>> A = [2 3 4; 5 6 7] ;>> B = 3;>> A + Bans =

5 6 78 9 10

A and B must be of the same size or a scalar.

>> B = [1 2 3; 4 5 6] ;>> A + Bans =

3 5 79 11 13

Page 16: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-16

>> A = [2 3 4; 5 6 7; 8 9 10] ;>> B = 3;>> A - Bans =

-1 0 12 3 45 6 7

A and B must be of the same size or a scalar.

>> B = [1 2 3; 4 5 6; 7 8 9] ;>> A - Bans =

1 1 11 1 11 1 1

Page 17: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-17

>> A = [2 3 4; 5 6 7] ;>> B = [1 2 3; 4 5 6] ;>> A .* Bans =

2 6 12 20 30 42

Note: the result is theproduct of the individualelements of the array.

The size of A and B must be the same or either could be a scalar .

For the other operators in the table on slide 4 - 14 , try to determine what the operator does and the sizes for whichA op B is valid. We will consider the “ \ “ operator in the future.

Page 18: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-18

The dot or scalar product of two vectors F * s is:

332211

3

2

1

321 sfsfsf

s

s

s

fff

sF

To compute the product “ * ” of two matrices A * B we will take the scalar products of the rows of A with the columns of B. If A has size (m x n) and B has size (n x p) then there are m x p possible combinations.

In fact A * B is defined only when the number of columns of A equals the number of rows of B.

Page 19: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-19

Given the two matrices (math notation):

232221

131211

aaa

aaaA

3231

2221

1211

bb

bb

bb

B

Page 20: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-20

=C(m x p) A (m x n) * B (n x p)

The product of matrix A and B is another matrix C. The size of C is m x p (the number of combinations of scalar products of the rows of A with the columns of B)

2221

1211

cc

ccC

Page 21: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-21

The possible combinations of scalar products of rows and columns are:

( c11 = first row of A * first column of B)

( c12 = first row of A * second column of B)

32

22

12

13121112

b

b

b

aaac

31

21

11

13121111

b

b

b

aaac

Page 22: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-22

The possible combinations of scalar products of rows and columns are:

( c21 = second row of A * first column of B)

( c22 = second row of A * second column of B)

31

21

11

23222121

b

b

b

aaac

32

22

12

23222122

b

b

b

aaac

Page 23: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-23

Example:

>> A = [2 3 4; 5 6 7] ;>> B = [1 2 3; 4 5 6; 7 8 9];>> C = A * BC =

42 51 6078 96 114

Note: the size of A * B is computed by:size(A) is 2 x 3 size(B) is 3 x 3size(A * B) is 2 x 3

Note: In general A * B does not equal B * A.

Page 24: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-24

>> A = [2 3 4; 5 6 7] ;>> B = [1 ; 2 ; 3] ;>> C = A * BC =

2038

Example:

Note: again, the size of A * B is computed by:size(A) is 2 x 3 size(B) is 3 x 1size(A * B) is 2 x 1

Page 25: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-25

Consider the three equations in three unknowns(below):

100x + 10y + z = 1225x + 15y + z = 20400x + 20y + z = 10

This system of three equations is called linear since the powers on all the variables are one and each variable occurs separately (i.e. no xy or xyz terms).

The plot of each equation in 3D is a plane. If the three planes intersect at one point in 3D space, then what is that point?

Page 26: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-26

To solve a system of equations A*B = C , i.e. to find the point of intersection of the three planes, use the “ \ ” operator.

>> A = [100 10 1; 225 15 1; 400 20 1] ;>> C = [1 ; 20 ; 10] ;>> B = A \ CB =

-0.5800 18.3000 -124.0000You can test this is the solution by typing>>A*B

1.0000 20.0000 10.0000

A is called the “coefficient matrix” for the system of linear equations.

The three planes intersect at the point (-0.5800, 18.3000, -124.0000)

Page 27: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-27

If we naively wanted to solve the equation for B given A and C A * B = C

(and we forgot that A is a matrix, B and C column vectors),then we might try to solve the above for B by dividing bothsides by A.

A\(A*B) = A\C where we read the black-slash A\C means A divided into C(A is underneath C). If A\(A*B) = (A\A)*B then since A\A is one we get

B = A\CWe can use this heuristic to remember how to use back-slash.

The “ \ ” back-slash operator uses a combination of numerical methods including LU decomposition.

Page 28: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

-VVVV-

-VVVV-

-VVVV-

-VVVV-

-VVVV-

-VVVV-

Problem: Weights suspended by springs

W5

W4

W3

W2

W1

K6

K5

K4

K3

K2

K1

Hooke’s Law: F = - K*ddi is the length of spring Si,Ki is the i-th spring constant,Wi is the i-th weight,hi is i-th vertical position

d1 + d2 + d3 + d4 + d5 +d6 = H,height of floor to ceiling.Also, h1 = d1 and hi = d1+d2+ ...+di for i = 2, ... , 5 .

From Newton’s second Law:Ki+1*di+1 - Ki*di = Wi for i=1 , ... ,5 .

If we can find the di values then wecan compute the hi values.

unknowns:

ceiling

floor

H

Page 29: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

For a system with five weightsK2*d2 - K1*d1 = W1

K3*d3 - K2*d2 = W2

K4*d4 - K3*d3 = W3

K5*d5 - K4*d4 = W4

K6*d6 - K5*d5 = W5

and

d1 + d2 + d3 + d4 + d5 +d6 = H

In math notation:

H

W

W

W

W

W

d

d

d

d

d

d

KK

KK

KK

KK

KK

5

4

3

2

1

6

5

4

3

2

1

65

54

43

32

21

111111

0000

0000

0000

0000

0000

Page 30: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-30

Construct the following vectors.K = [K1 ... K6];W = [W1 ... W5 H] ’ ;Form the matrix A in three steps1) create a “diagonal” matrix>> A = diag(-K,0);A now has the form:

6

5

4

3

2

1

00000

00000

00000

00000

00000

00000

K

K

K

K

K

K

Page 31: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-31

Form the matrix A in steps2) change the bottom row to a row of 1’s.>> A(end, : ) = 1;A now has the form:

111111

00000

00000

00000

00000

00000

5

4

3

2

1

K

K

K

K

K

Page 32: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-32

Form the matrix A in steps3) Put in the values above the diagonal.>> A = A + diag( K(2:end) , 1 );A now has the form:

111111

0000

0000

0000

0000

0000

65

54

43

32

21

KK

KK

KK

KK

KK

Page 33: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-33

Solve the linear systemA * D = W by using the back-slash operator “ \ ”

>> D = A \ W;

and finally, since [h1 , h2 h3 , h4 , h5 , h6 ] = [d1 ,d1+d2 ,d1+d2+d3 , d1+d2+d3+d4 , ... ]

>> H = cumsum(D);

Page 34: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-34

The cumsum function returns the cumulative sum of values for a vector. For a matrix, cumsum operates on the columns of the matrix.

Example >> p1 = [1 -2 5];>> mat1 = [1 -2 ;1 3 ; 2 4] ;>> cumsum(p1)ans =

1 -1 4>> cumsum(mat1)ans =

1 -2 2 1 4 5

Page 35: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.
Page 36: 4-2 How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions.

4-36

Matlab arrays can be vectors or matrices. You can construct matrices using the semicolon operator “ ; ” which means append values to next row. The functions eye, ones, zeros and diag can also be used to create matrices.

You can use subscripting on matrices. Use the colon “ : ” to denote all of the row or column.

The back-slash operator “ \ ” is used in obtaining solutions to linear systems of equations.

The end command can be used in subscripting to return the last row or column of the matrix (or vector).