Upload
eitan
View
45
Download
0
Tags:
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
MATLAB – Ch 2 - Numeric, Cell, & Structure ArraysEGR1302
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
Allows for complex operations using one command or function
Means that Matlab programs can be very short
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
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
ARRAYSSection 2.1
Cartesian coordinates review Represent a point p in
3D space x, y, & z
Represent unit vectors
kjii 001ˆ kjij 010ˆ
kjik 100ˆ
Cartesian coordinates review Represent a vector p
from origin to point p
In Matlab Row vector
Column vector
kzjyixp ˆˆˆ
zyx
p
],,[ zyxp
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
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]’
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
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
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
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
2D Arrays Square brackets denote matrices
Recall Parallel lines denote a determinant
174352
M
3042
N
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
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
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
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)
Vector terms Length
Number of elements in a vector Magnitude
Vector’s geometric length Absolute value
Absolute values of each elements in vector
Array editor Graphical interface for working with
arrays View and edit workspace variables Clear workspace variables Plotting workspace variables
MULTIDIMENSIONAL ARRAYS
Section 2.2
3D & 4D arrays 1st dimension is row 2nd dimension is column Higher dimensions are referred
to as pages
ELEMENT-BY-ELEMENT OPERATIONS
Section 2.3
Scalar multiplication Increase
magnitude of a vector by multiplying it by scalar
r=[3,5,2];>> v=2*r
v =
6 10 4
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
Array addition & subtraction Associative
Commutative
)()( CBACBA
BCAACBCBA
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
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
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
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
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
MATRIX OPERATIONSSection 2.4
Addition & Subtraction Matrix addition and subtraction
are identical to element-by-element addition and subtracted
Vector Multiplication Vector dot product
Recall result is a scalar332211
)cos( wuwuwuwuwu
1ˆˆˆˆˆˆ kkjjii0ˆˆˆˆˆˆ kjkiji
332211
3
2
1
321wuwuwu
www
uuu
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
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
=
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
POLYNOMIAL OPERATIONS USING ARRAYS
Section 2.5
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
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
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.