matlab chapter 2: array and matrix operations. what is a vector? in matlab, it is a single row...

28
Matlab Matlab Chapter 2: Array and Chapter 2: Array and Matrix Operations Matrix Operations

Upload: angela-claire-baldwin

Post on 30-Dec-2015

242 views

Category:

Documents


0 download

TRANSCRIPT

MatlabMatlabChapter 2: Array and Matrix Chapter 2: Array and Matrix

OperationsOperations

What is a vector?What is a vector? In Matlab, it is a single row (horizontal) or column (vertical) of numbers or characters.In Matlab, it is a single row (horizontal) or column (vertical) of numbers or characters.

Vector is one row OR one columnVector is one row OR one column– Does not have to have anything to do with geometryDoes not have to have anything to do with geometry– Number of components is not restricted Number of components is not restricted – Transpose of row is column, of column is rowTranspose of row is column, of column is row

99287261,43

...

cba

,

Vectors in MatlabVectors in Matlab

Various forms for entering row and Various forms for entering row and column vectors:column vectors:

>> p=[1,2,3]>> p=[1,2,3]p =p =

1 2 31 2 3

>> u=[1;2;3;4]u = 1 2 3 4>> v=[3:2:9]

v =

3 5 7 9

>> w=[4:7]

w =

4 5 6 7

Transposing VectorsTransposing Vectors

>> p>> pp =p = 1 2 31 2 3>> p'>> p'ans =ans = 11 22 33

>> u>> u

u =u =

11

22

33

44

>> u'>> u'

ans =ans =

1 2 3 41 2 3 4

Augmenting Row VectorsAugmenting Row Vectors

>> p>> pp =p = 1 2 31 2 3>> v>> vv =v = 3 5 7 93 5 7 9>> z=[p v]>> z=[p v]z =z = 1 2 3 3 5 7 91 2 3 3 5 7 9

Stacking Column VectorsStacking Column Vectors

>> u>> u

u =u =

11

22

33

44

>> t

t =

3

8

-4

>> q=[u;t]

q =

1

2

3

4

3

8

-4

Functions to Generate VectorsFunctions to Generate Vectors

linspace(a,b,c) produces c evenly spaced points linspace(a,b,c) produces c evenly spaced points between a and bbetween a and b

>> linspace(2,4,5)>> linspace(2,4,5)ans =ans = 2.0000 2.5000 3.0000 3.5000 4.00002.0000 2.5000 3.0000 3.5000 4.0000 logspace(a,b,n) computes n pts linearly between a and

b, then uses them as exponents of 10>> logspace(1,3,4)ans = 1.0e+003 * 0.0100 0.0464 0.2154 1.0000

MatricesMatrices A rectangular array of numbers or charactersA rectangular array of numbers or characters

– Rows numbered top to bottomRows numbered top to bottom– Columns numbered left to rightColumns numbered left to right– Size of array: n x mSize of array: n x m

» number of rows is always stated firstnumber of rows is always stated first

– element of array: x(k,j)element of array: x(k,j)» row index is always stated first—kth row, jth colrow index is always stated first—kth row, jth col

– In generalIn general, indices can be negative or zero, but not , indices can be negative or zero, but not in in MATLABMATLAB

– Transpose of real matrix interchanges rows and Transpose of real matrix interchanges rows and columnscolumns

Transpose & Concatenate Transpose & Concatenate MatricesMatrices

>> A=[1,2,3;4,5,6] >> A=[1,2,3;4,5,6] A =A = 1 2 31 2 3 4 5 64 5 6

A’ is transposeA’ is transpose>> A'>> A'ans =ans = 1 41 4 2 52 5 3 63 6

>> B=[4,5;8,9]>> B=[4,5;8,9]B =B = 4 54 5 8 98 9[A,B] augments (concatenates rows)[A,B] augments (concatenates rows)ans =ans = 1 2 3 4 51 2 3 4 5 4 5 6 8 94 5 6 8 9[A’;B] stacks (concatenates cols)[A’;B] stacks (concatenates cols)ans =ans = 1 41 4 2 52 5 3 63 6 4 54 5 8 98 9

Vector AddressingVector Addressing

» v = [1,7,3,8,6,7,3]v = [1,7,3,8,6,7,3]

• v(4) returns the 4v(4) returns the 4thth element element>> v(4)>> v(4)

ans = 8ans = 8

Note: parentheses, not square Note: parentheses, not square brackets!brackets!

• v(2:5) returns elements 2 through 5v(2:5) returns elements 2 through 5>> v(2:5)>> v(2:5)ans = 7 3 8 6ans = 7 3 8 6

Vector Addressing cont.Vector Addressing cont.

» v = [1,7,3,8,6,7,3]v = [1,7,3,8,6,7,3]

• v(:) returns the entire vector as a columnv(:) returns the entire vector as a column>> v(:)>> v(:)

ans =ans =

11 77 33 88 66 77 33

Matrix AddressingMatrix AddressingA =A = 3 6 83 6 8 1 5 21 5 2

A(2,1) returns the element in the 2A(2,1) returns the element in the 2ndnd row, 1 row, 1stst column column» ans = 1ans = 1

A(:,3)A(:,3) returns the 3 returns the 3rdrd column column» ans = 8ans = 8

2 2

A(2,:)A(2,:) returns the 2 returns the 2ndnd row row» ans = 1 5 2 ans = 1 5 2

A(1:2,2:3) returns all elements in the 1A(1:2,2:3) returns all elements in the 1stst and 2 and 2ndnd rows rows that are also in 2that are also in 2ndnd and 3 and 3rdrd columns columns

Matrix AddressingMatrix AddressingA =A = 3 6 83 6 8 1 5 21 5 2 A(1:2,2:3) returns all elements in the 1A(1:2,2:3) returns all elements in the 1stst and and

22ndnd rows that are also in 2 rows that are also in 2ndnd and 3 and 3rdrd columns columnsans = 6 8ans = 6 8

5 25 2 Extract smaller array:Extract smaller array:

» B = A(1:2,[1 3])B = A(1:2,[1 3])B = 3 8B = 3 8 1 2 1 2

Arrays/Matrix AddressingArrays/Matrix Addressing

Empty or null arrayEmpty or null array» [ ][ ]

A =A = 3 6 83 6 8 1 5 21 5 2

– Remove a column: Remove a column: » A(:,3) = [ ]A(:,3) = [ ]

A = 3 6A = 3 6 1 5 1 5

– Automatic enlargement:Automatic enlargement:» A(3,5) = 22A(3,5) = 22

A= 3 6 0 0 0A= 3 6 0 0 0 1 5 0 0 01 5 0 0 0

0 0 0 0 22 0 0 0 0 22

Arrays/Matrix AddressingArrays/Matrix Addressing

– Negative increment reverses order:Negative increment reverses order:

p=[3,8,4,6];p=[3,8,4,6];

» rev = p(end:-1:1)rev = p(end:-1:1)rev = 6 4 8 3rev = 6 4 8 3

– A(n) returns the nA(n) returns the nthth element of matrix A, going element of matrix A, going column by columncolumn by column

A = 3 6 8A = 3 6 8

1 5 21 5 2

» A(4)A(4)ans = 5ans = 5

Arrays/Matrix AddressingArrays/Matrix Addressing

A(:) returns all elements as a column, A(:) returns all elements as a column, going column by columngoing column by column A = 3 6 8A = 3 6 8 1 5 2 1 5 2

» A(:)A(:)ans = 3ans = 3 1 1 6 6 5 5 8 8 2 2

Automatic growth of a vectorAutomatic growth of a vector

If vector already defined as row or column, assigning If vector already defined as row or column, assigning new element beyond current bounds new element beyond current bounds automaticallyautomatically grows to fit with intervening elements set to 0grows to fit with intervening elements set to 0

» x = [1;2]x = [1;2] % x starts as 2-element column vector% x starts as 2-element column vector

» x(5) = 24x(5) = 24 % x now has 5 elements. x(3) & x(4) = 0% x now has 5 elements. x(3) & x(4) = 0x =x =

11 22 00 00 2424

Automatic growth of a vector:Automatic growth of a vector:– If variable is not yet vector, assigning element If variable is not yet vector, assigning element

beyond 1 defaults to creating a row vectorbeyond 1 defaults to creating a row vector

>>clear xx>>clear xx» xx(4) = 12xx(4) = 12 % xx starts as a 4-element row vector% xx starts as a 4-element row vector

xx =xx =

0 0 0 120 0 0 12

Warning!Warning!

Use clear to Avoid ErrorsUse clear to Avoid Errors– Be careful:Be careful:

suppose you simply want A to have u and v as columns:suppose you simply want A to have u and v as columns:

» A(:,1) = u;A(:,1) = u;» A(:,2) = v;A(:,2) = v;

– A however, A however, stillstill has its previous columns! has its previous columns!

– Use clear to avoid this:Use clear to avoid this:» clear Aclear A

Vector and Matrix FunctionsVector and Matrix Functions

CommandCommand DescriptionDescriptioncat(n,A,B,C, …)cat(n,A,B,C, …) concatenates A,B,C,… along dimension nconcatenates A,B,C,… along dimension n

find(x)find(x) indices of nonzero elements of array xindices of nonzero elements of array x

length(A)length(A) vector vector # of elements, matrix # of elements, matrix max. of max. of mm or or nn

max(A)max(A) matrix matrix largest elements of each column largest elements of each column

[x,k] = max(A)[x,k] = max(A) stores max.’s in x, indices in kstores max.’s in x, indices in k

min(A)min(A) same as max but minimum valuessame as max but minimum values

[x,k] = min(A)[x,k] = min(A) same as [x,k] = max(A) but minimum valuessame as [x,k] = max(A) but minimum values

size(A)size(A) returns dimensions of array Areturns dimensions of array A

sort(A)sort(A) sorts each column in ascending ordersorts each column in ascending order

sum(A)sum(A) returns vector of column sumsreturns vector of column sums

Array OperationsArray Operations Same operation performed on each corresponding Same operation performed on each corresponding

element of arrayelement of arrayA =A = 3 7 23 7 2 6 -1 56 -1 5>> A+3>> A+3ans =ans = 6 10 56 10 5 9 2 89 2 8>> 2*A>> 2*Aans =ans = 6 14 46 14 4 12 -2 1012 -2 10>> A.^2>> A.^2ans =ans = 9 49 49 49 4 36 1 2536 1 25

B =B = 4 6 14 6 1 3 9 23 9 2>> A+B>> A+Bans =ans = 7 13 37 13 3 9 8 79 8 7>> A.*B>> A.*Bans =ans = 12 42 212 42 2 18 -9 1018 -9 10A./BA./Bans =ans = 0.7500 1.1667 2.00000.7500 1.1667 2.0000 2.0000 -0.1111 2.50002.0000 -0.1111 2.5000

Math Functions Automatically Math Functions Automatically Apply to Each ElementApply to Each Element

A =

3 7 2

6 -1 5

>> sin(A)

ans =

0.1411 0.6570 0.9093

-0.2794 -0.8415 -0.9589

>> sqrt(A)

ans =

1.7321 2.6458 1.4142

2.4495 0 + 1.0000i 2.2361

Vector OperationsVector Operations

u =u =

3 2 53 2 5

v =

-4 5 2

>> dot(u,v)

ans =

8

>> cross(u,v)

ans =

-21 -26 23

>> norm(u)

ans =

6.1644

>> %this is the Euclidean length or magnitude

Matrix ProductMatrix Product

C= A*B is defined if and only if the C= A*B is defined if and only if the number of columns in A equals the number of columns in A equals the number of rows in B. Then C will have number of rows in B. Then C will have the same number of rows as A and the the same number of rows as A and the same number of columns as Bsame number of columns as B

(3x2)*(2x4) produces a 3x4(3x2)*(2x4) produces a 3x4

(3x2)*(3x2) not defined(3x2)*(3x2) not defined

(3x2)*(2x3) produces a 3x3(3x2)*(2x3) produces a 3x3

(2x3)*(3x2) produces a 2x2(2x3)*(3x2) produces a 2x2

Matrix Product C=A*BMatrix Product C=A*B

The (i,j) entry in C is the sum of the products of entries The (i,j) entry in C is the sum of the products of entries from the i-th row of A and the j-th column of Bfrom the i-th row of A and the j-th column of B

A =

4 5 -1

3 2 0

B =

3 2 1 5

1 -1 0 3

2 5 1 4>> A*B

ans =

15 -2 3 31

11 4 3 21

Matrix PowerMatrix Power

A^n means multiply A by itself n times.A^n means multiply A by itself n times.– Can only be done if A is squareCan only be done if A is square

A.^n means raise each element of A to A.^n means raise each element of A to the n-th powerthe n-th power– Can be done with any array ACan be done with any array A

Matrix DivisionMatrix Division

If A and B are matrices, A/B is NOT a defined If A and B are matrices, A/B is NOT a defined operation in linear algebra!operation in linear algebra!

Matlab uses both right (/) and left (\) operator Matlab uses both right (/) and left (\) operator symbols for special matrix operations we are symbols for special matrix operations we are not covering in this course!not covering in this course!

1./A is an array operation creating a new array 1./A is an array operation creating a new array with each entry being the reciprocal of the with each entry being the reciprocal of the entry in Aentry in A

A./B is an array operation creating a new array A./B is an array operation creating a new array with each entry being the corresponding entry with each entry being the corresponding entry in A divided by the corresponding entry in Bin A divided by the corresponding entry in B

Special MatricesSpecial Matrices

eye(n)—nxn “identity” matrixeye(n)—nxn “identity” matrix– eye(3)eye(3)

1 0 01 0 0

0 1 00 1 0

0 0 10 0 1

ones(m,n)– mxn matrix with all entries = 1ones(m,n)– mxn matrix with all entries = 1 zeros(m,n)—mxn matrix with all entries =0zeros(m,n)—mxn matrix with all entries =0