Matlab Chapter 2

advertisement
MATLAB – Ch 2 - Numeric, 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
 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
u  0 : 0.1 : 10
Introduction

Arrays in Chapter 1

Use just variable name in expressions
 Operation
the array

is performed on every element in
Use variable name & array index in
expressions
u (7)

Use length function to determine
number of elements in an array
Section 2.1
ARRAYS
Cartesian coordinates review

Represent a point p in 3D
space


x, y, & z
Represent unit vectors
iˆ  1i  0 j  0k
ˆj  0i  1 j  0k
kˆ  0i  0 j  1k
Cartesian coordinates review

Represent a vector p
from origin to point p
p  xiˆ  yˆj  zkˆ

In Matlab
p  [ x, y, z ]

Row vector

Column vector
x 
p   y
 
 z 
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

>>p = [3;7;9]
p =
3
7
9

OR transpose a row vector
>>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 u  x1 : d : x 2
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)
u  linspace( x1, x2, n)
Generating vectors of
regularly spaced elements

Use logspace function
u  logspace(a, b, n)
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)
2D Arrays

Array


Row vector


Array with a single row of scalars
Column vector


Collection of scalars arranged in logical
manner
Array with a single column of scalars
Matrix

An array with multiple rows and columns
2D Arrays

Square brackets denote matrices
 2 5
M    3 4


 7 1 

Recall  Parallel lines denote a
determinant
2 4
N
0 3
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
16
4
3
10
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(:)
v
 all row or column elements in vector
 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
 v(2:4)
Useful array functions
See Table 2.1-1, p. 77
 Max

>> y = max(x)

For a vector x, returns algebraically
greatest element
>> [x,k] = max(B)

For a matrix B, returns
vector x containing greatest element in
each column of B
 Row vector k containing indices of greatest
elements in each column of B
 Row
Vector terms

Length


Magnitude


Number of elements in a vector
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
Section 2.2
MULTIDIMENSIONAL
ARRAYS
3D & 4D arrays
1st dimension is row
 2nd dimension is column
 Higher dimensions are referred to as
pages

Section 2.3
ELEMENT-BY-ELEMENT
OPERATIONS
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

>> A=[6,-2;10,3];
>> B=[9,8;-12,14];
>> C=A+B
C=
15 6
-2 17
c  a b
ij
ij
ij
Array addition & subtraction

Associative
( A  B)  C  A  ( B  C )

Commutative
A B C  B C  A  AC  B
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.85
Symbol
Operation
Form
Examples
+
Scalar-array addition
A + b
[6,3]+2=[8,5]
-
Scalar-array subtraction A – b
+
Array addition
A + B
[6,5]+[4,8]=[10,13]
-
Array subtraction
A – B
[6,5]-[4,8]=[2,-3]
.*
Array multiplication
A.*B
[3,5].*[4,8]=[12,40]
./
Array right division
A./B
[2,5]./[4,8]=[2/4,5/8]
.\
Array left division
A.\B
[2,5].\[4,8]=[2\4,5\8]
.^
Array exponentiation
A.^B
[3,5].^2=[3^2,5^2]
[8,3]-5=[3,-2]
2.^[3,5]=[2^3,2^5]
[3,5].^[2,4]=[3^2,5^4]
Array multiplication

Vectors must be of the same size
x. * y  [ x(1) y(1), x(2) y(2),..., x(n) y(n)]

Matrices must be of the same size
c  a b
ij

ij
ij
Dot (.) and asterisk (*) form one
symbol
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 type
z  exp( y). * sin( x). * (cos( x)).^2
Section 2.4
MATRIX OPERATIONS
Addition & Subtraction

Matrix addition and subtraction are
identical to element-by-element
addition and subtracted
Vector Multiplication

Vector dot product
u  w  u w cos( )  u w u w  u w
1

1
2
2
3
3
Recall  result is a scalar
iˆ  iˆ  ˆj  ˆj  kˆ  kˆ  1
iˆ  ˆj  iˆ  kˆ  ˆj  kˆ  0
u
1
w 
u  w   u w u w  u w
 
 w 
1
u
2
3
2
3
1
1
2
2
3
3
Vector-Matrix Multiplication

Matrix multiplied by column vector
a
a

11
21
a  x  a x  a x 





a   x  a x  a x 
12
1
11
1
12
2
22
2
21
1
22
2
Result is a column vector
 Number of columns in matrix must equal
number of rows in vector

Matrix multiplication

To multiply two matrices A & B
Number of columns in A must equal
number of rows in
 The resulting product AB has

number of rows as A
 Same number of columns as B
 Same
6
10
4
–2
3
7
9
–5
8
12
=
(6)(9) + (– 2)(– 5)
(10)(9) + (3)(– 5)
(4)(9) + (7)(– 5)
=
64
75
1
24
116
116
(6)(8) + (– 2)(12)
(10)(8) + (3)(12)
(4)(8) + (7)(12)
Matrix multiplication

Matrix multiplication is NOT
commutative

The order of the matrices in the
equation is important
A B  B  A

Exceptions
matrix 0
 Identity matrix I
 Null
0 A  A0  0
IA  AI  A
Section 2.5
POLYNOMIAL OPERATIONS
USING 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
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.

Download