43
MATLAB – Ch 2 - Numeric, Cell, & Structure Arrays EGR1302

MATLAB – Ch 2 - N umeric, Cell, & Structure Arrays

  • Upload
    eitan

  • View
    45

  • Download
    0

Embed Size (px)

DESCRIPTION

MATLAB – Ch 2 - N umeric, Cell, & Structure Arrays. EGR1302. Outline. Introduction Arrays Multidimensional arrays Element-by-element operations Polynomial operations using arrays. Introduction. Array capabilities in Matlab Serves as basic building block in Matlab - PowerPoint PPT Presentation

Citation preview

Page 1: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

MATLAB – Ch 2 - Numeric, Cell, & Structure ArraysEGR1302

Page 2: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Outline Introduction Arrays Multidimensional arrays Element-by-element operations Polynomial operations using

arrays

Page 3: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Introduction Array capabilities in Matlab

Serves as basic building block in Matlab

Allows for complex operations using one command or function

Means that Matlab programs can be very short

Page 4: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Introduction Arrays in Chapter 1

Array assignment “[]” contains numbers being assigned Commas or spaces separate elements

in row Semi-colons separate rows

Special format for assigning row array with regularly spaced numbers 10:1.0:0u

Page 5: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Introduction Arrays in Chapter 1

Use just variable name in expressions

Operation is performed on every element in the array

Use variable name & array index in expressions

Use length function to determine number of elements in an array

)7(u

Page 6: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

ARRAYSSection 2.1

Page 7: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays
Page 8: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Cartesian coordinates review Represent a point p in

3D space x, y, & z

Represent unit vectors

kjii 001ˆ kjij 010ˆ

kjik 100ˆ

Page 9: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Cartesian coordinates review Represent a vector p

from origin to point p

In Matlab Row vector

Column vector

kzjyixp ˆˆˆ

zyx

p

],,[ zyxp

Page 10: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Creating vectors in Matlab To create a row vector

Type numbers within square brackets

Separate numbers with a space or a comma>>p = [3,7,9]

p = 3 7 9

Page 11: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Creating vectors in Matlab To create a column vector

Type numbers within square brackets

Separate numbers with a semi-colon

OR transpose a row vector

>>p = [3;7;9]p = 3 7 9

>>p = [3,7,9]’

Page 12: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Appending one vector to another Append 2 row vectors

>> r = [2,4,20];>> w = [9,-6,3];>> u = [r,w] u =

2 4 20 9 -6 3

Page 13: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Generating vectors of regularly spaced elements Use colon “:” operator

x1 = first value in the series x2 = last value in the series d = increment between numbers in

series (default is 1 if d is omitted) Use linspace function

n =number of points (default is 1 if n is omitted)

2::1 xdxu

),2,1( nxxlinspaceu

Page 14: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Generating vectors of regularly spaced elements Use logspace function

a = exponent for first value (i.e., 10a)

b = exponent for last value (i.e., 10b)

n =number of points (default is 50 if n is omitted)

),,( nbaspacelogu

Page 15: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

2D Arrays Array

Collection of scalars arranged in logical manner

Row vector Array with a single row of scalars

Column vector Array with a single column of

scalars Matrix

An array with multiple rows and columns

Page 16: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

2D Arrays Square brackets denote matrices

Recall Parallel lines denote a determinant

174352

M

3042

N

Page 17: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Creating Matrices Type row by row

Semi-colon separating rows Comma or space separating

elements in a row>> A = [2,4,10;16,3,7]

A =

2 4 10 16 3 7

Page 18: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Array addressing v(5) 5th element in vector v A(2,3) Element in 2nd row and

3rd column in array A Row number is always first!

D(1,3) = 4 Replaces the element in 1st row, 3rd

column of array D with 4

Page 19: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Array addressing Colon operator

Selects submatrices v(:) all row or column elements in

vector v v(2:4) 2nd through 4th elements in v A(3,:) all elements in the 3rd row of

matrix A A( :,2:4) all elements in the 2nd

through 4th columns of A B(2:4,1:3) all elements in the 2nd

through 4th rows and 1st through 3rd columns of B

Page 20: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Useful array functions See Table 2.1-1, p. 77 Max

For a vector x, returns algebraically greatest element

For a matrix B, returns Row vector x containing greatest

element in each column of B Row vector k containing indices of

greatest elements in each column of B

>> y = max(x)

>> [x,k] = max(B)

Page 21: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Vector terms Length

Number of elements in a vector Magnitude

Vector’s geometric length Absolute value

Absolute values of each elements in vector

Page 22: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Array editor Graphical interface for working with

arrays View and edit workspace variables Clear workspace variables Plotting workspace variables

Page 23: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

MULTIDIMENSIONAL ARRAYS

Section 2.2

Page 24: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

3D & 4D arrays 1st dimension is row 2nd dimension is column Higher dimensions are referred

to as pages

Page 25: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

ELEMENT-BY-ELEMENT OPERATIONS

Section 2.3

Page 26: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Scalar multiplication Increase

magnitude of a vector by multiplying it by scalar

r=[3,5,2];>> v=2*r

v =

6 10 4

Page 27: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Array addition & subtraction 2 arrays with same

size Sum or difference has

same size Add or subtract

corresponding elements

C =

15 6 -2 17 ijijij

bac

>> A=[6,-2;10,3];>> B=[9,8;-12,14];>> C=A+B

Page 28: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Array addition & subtraction Associative

Commutative

)()( CBACBA

BCAACBCBA

Page 29: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Multiplication of two arrays Two definitions of multiplication

of two arrays Array multiplication

Element-by-element operation Matrix multiplication

Division and exponentiation also must be carefully defined

Page 30: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Table 2.3-1, p.85Symbol

+

-

+

-

.*

./

.\

.^

Examples

[6,3]+2=[8,5]

[8,3]-5=[3,-2]

[6,5]+[4,8]=[10,13]

[6,5]-[4,8]=[2,-3]

[3,5].*[4,8]=[12,40]

[2,5]./[4,8]=[2/4,5/8]

[2,5].\[4,8]=[2\4,5\8]

[3,5].^2=[3^2,5^2]

2.^[3,5]=[2^3,2^5]

[3,5].^[2,4]=[3^2,5^4]

Operation

Scalar-array addition

Scalar-array subtraction

Array addition

Array subtraction

Array multiplication

Array right division

Array left division

Array exponentiation

Form

A + b

A – b

A + B

A – B

A.*B

A./B

A.\B

A.^B

Page 31: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Array multiplication Vectors must be of the same size

Matrices must be of the same size

Dot (.) and asterisk (*) form one symbol

)]()(),...,2()2(),1()1([*. nynxyxyxyx

ijijijbac

Page 32: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Built-in Matlab functions sqrt(x) and exp(x)

Automatically operate on array arguments to produce an array result the same size as the array argument.

Thus these functions are said to be vectorized functions

Page 33: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Built-in Matlab functions When multiplying, dividing, or

exponentiating these functions, you must use element-by-element operations if the arguments are arrays. To compute z = (ey sin x) cos2x,

you must type2)).^(cos(*).sin(*).exp( xxyz

Page 34: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

MATRIX OPERATIONSSection 2.4

Page 35: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Addition & Subtraction Matrix addition and subtraction

are identical to element-by-element addition and subtracted

Page 36: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Vector Multiplication Vector dot product

Recall result is a scalar332211

)cos( wuwuwuwuwu

1ˆˆˆˆˆˆ kkjjii0ˆˆˆˆˆˆ kjkiji

332211

3

2

1

321wuwuwu

www

uuu

Page 37: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Vector-Matrix Multiplication Matrix multiplied by column

vector

Result is a column vector Number of columns in matrix must

equal number of rows in vector

222121

212111

2

1

2221

1211

xaxaxaxa

xx

aaaa

Page 38: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Matrix multiplication To multiply two matrices A & B

Number of columns in A must equal number of rows in

The resulting product AB has Same number of rows as A Same number of columns as B

6 –2 10 3 4 7

9 8–5 12

= (6)(9) + (– 2)(– 5) (6)(8) + (– 2)(12) (10)(9) + (3)(– 5) (10)(8) + (3)(12) (4)(9) + (7)(– 5) (4)(8) + (7)(12)

64 24 75 116 1 116

=

Page 39: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Matrix multiplication Matrix multiplication is NOT

commutative The order of the matrices in the

equation is important

Exceptions Null matrix 0 Identity matrix I

ABBA

AAIIAAA

000

Page 40: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

POLYNOMIAL OPERATIONS USING ARRAYS

Section 2.5

Page 41: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Polynomials in Matlab Defined as row vector

Containing coefficients Starting with coefficient of highest

power of x Addition & subtraction

Add row vectors BUT if polynomials are of different

degrees, add zeros to coefficient array of lower degree polynomial

Fool Matlab into thinking that the lower degree polynomial has the same degree

Page 42: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Polynomial functions Roots(a) calculate roots Poly(a) computes coefficients

of polynomial whose roots are contained in a

See Table 2.5-1 on p.108 for additional functions

Page 43: MATLAB  – Ch 2 - N umeric, Cell, & Structure Arrays

Plotting a polynomial Function polyval(a,x)

Evaluates a polynomial at specified values of its independent variable x, which can be a matrix or a vector.

The polynomial’s coefficients of descending powers are stored in the array a.

The result is the same size as x.