L14. Arrays and Functions - PowerPoint PPT Presentation

1 / 56
About This Presentation
Title:

L14. Arrays and Functions

Description:

L14. Arrays and Functions Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand, randn, max – PowerPoint PPT presentation

Number of Views:95
Avg rating:3.0/5.0
Slides: 57
Provided by: CSCom1
Category:

less

Transcript and Presenter's Notes

Title: L14. Arrays and Functions


1
L14. Arrays and Functions
  • Functions with array
  • parameters.
  • Row and column vectors
  • Built-Ins length, zeros, std
  • Revisit rand, randn, max

2
Row and Column Vectors
  • gtgt v 1 2 3
  • v
  • 1 2 3
  • gtgt v 1 2 3
  • v
  • 1
  • 2
  • 3

Observe semicolons
3
zeros( , )
  • gtgt x zeros(3,1)
  • x
  • 0
  • 0
  • 0
  • gtgt x zeros(1,3)
  • x
  • 0 0 0

4
rand( , )
  • gtgt x rand(3,1)
  • x
  • 0.2618
  • 0.7085
  • 0.7839
  • gtgt x rand(1,3)
  • x
  • 0.9862 0.4733 0.9028

5
randn( , )
  • gtgt x randn(1,3)
  • x
  • 0.2877 -1.1465 1.1909
  • gtgt x randn(3,1)
  • x
  • 1.1892
  • -0.0376
  • 0.3273

6
Normal Distribution withZero Mean and Unit STD
7
Affirmations
  • gtgt n 1000000
  • gtgt x randn(n,1)
  • gtgt ave sum(x)/n
  • ave
  • -0.0017
  • gtgt standDev std(x)
  • standDev
  • 0.9989

8
length
  • gtgt v randn(1,5)
  • gtgt n length(v)
  • n
  • 5
  • gtgt u rand(5,1)
  • gtgt n length(u)
  • n
  • 5

The length function doesnt care about row or
column orientation.
9
Augmenting Row Vectors
  • gtgt x 10 20
  • x
  • 10 20
  • gtgt x x 30
  • x
  • 10 20 30
  • gtgt

10
Augmenting Column Vectors
  • gtgt x 1020
  • x
  • 10
  • 20
  • gtgt x x 30
  • x
  • 10
  • 20
  • 30

Observe semicolons!
11
Concatenating Row Vectors
  • gtgt x 10 20
  • x
  • 10 20
  • gtgt y 30 40 50
  • y
  • 30 40 50
  • gtgt z x y
  • z
  • 10 20 30 40 50

12
Concatenating Column Vectors
  • gtgt x 10 20
  • gtgt y 30 40 50
  • gtgt z x y
  • z
  • 10
  • 20
  • 30
  • 40
  • 50

Observe semicolons!
13
Application
Plot sine across 0,4pi and use the fact that
it has period 2pi.
  • x linspace(0,2pi,100)
  • y sin(x)
  • x x x2pi
  • y y y
  • plot(x,y)

14
x linspace(0,2pi,100) x x x2pi
linspace(2pi,4pi,100)
15
The Empty Vector
  • x
  • for k150
  • if floor(sqrt(k))sqrt(k)
  • x x k
  • end
  • end
  • x x

x 1 4 9 16 25 36 49
16
Array Hints Errors
17
Dimension Mismatch
  • gtgt x 12
  • x
  • 1
  • 2
  • gtgt y 3 4
  • y
  • 3 4
  • gtgt z xy
  • ??? Error using gt plus
  • Matrix dimensions must agree.

Cant add a row vector to a column vector
18
Not a Syntax Error
  • gtgt x rand(3)
  • x
  • 0.9501 0.4860 0.4565
  • 0.2311 0.8913 0.0185
  • 0.6068 0.7621 0.8214

You probably meant to say x rand(1,3) or x
rand(3,1).
19
A Style Hint
  • Assume n is initialized.

a zeros(1,n) for k1n a(k) sqrt(k) end
a for k1n a a sqrt(k) end
Better because it reminds you of the size and
shape of the array you set up.
20
Error Out-ofRange Subscript
  • gtgt x 10 20 30
  • x
  • 10 20 30
  • gtgt c x(4)
  • ??? Index exceeds matrix dimensions.

21
This is OK
  • gtgt x 10 20 30
  • x
  • 10 20 30
  • gtgt x(4) 100
  • x
  • 10 20 30 100

22
Forgot the Semicolon?
  • gtgt x randn(1000000,1)

23
Forgot the Semicolon?
  • gtgt x randn(1000000,1)

Remember ctrl-C
24
Question Time
  • A 1
  • while(length(A) lt 5)
  • A length(A)1 A
  • end
  • A A
  • Is this the same as
  • A linspace(1,5,5) ?

A. Yes B. No
25
No!
  • Linspace
  • 1 2 3 4 5
  • Fragment
  • 5
  • 4
  • 3
  • 2
  • 1

26
Question Time
  • x zeros(1,1)
  • for k13
  • x x x
  • end
  • y x(7)
  • Will this cause a subscript out of
  • bounds error?

A. Yes B. No
27
No!
  • How x changes
  • After 1st pass 0 0
  • After 2nd pass 0 0 0 0
  • After 3rd pass 0 0 0 0 0 0 0 0
  • So y x(7) makes sense.

28
Polygon Transformations
Functions arrays
29
A Polygon
(x1,y1)
(x2,y2)
Store xy-coordinates in vectors x and y.
(x5,y5)
(x3,y3)
(x4,y4)
30
Operation 1 Centralize
  • Move a polygon so that the centroid
  • of its vertices is at the origin.

31
Centralize
Before
After
32
Centralize
  • function xNew,yNew Centralize(x,y)
  • n length(x)
  • xBar sum(x)/n
  • yBar sum(y)/n
  • xNew x-xBar
  • yNew y-yBar

Computes the vertices of the new polygon
Notice how length is used to figure out the
size of the incoming vectors.
33
Operation 2 Normalize
  • Shrink (enlarge) the polygon so that
  • the vertex furthest from the
  • (0,0) is on the unit circle.

34
Normalize
Before
After
35
Normalize
  • function xNew,yNew Normalize(x,y)
  • d max(sqrt(x.2 y.2))
  • xNew x/d
  • yNew y/d

Applied to a vector, max returns the largest
value in the vector.
36
Operation 3 Smooth
  • Obtain a new polygon by connecting
  • the midpoints of the edges

37
Smooth
38
Midpoints
(c,d)
( (ac)/2 , (bd)/2 )
(a,b)
39
Smooth
  • function xNew,yNew Smooth(x,y)
  • n length(x)
  • xNew zeros(n,1)
  • yNew zeros(n,1)
  • for i1n
  • Compute the mdpt of ith edge.
  • Store in xNew(i) and yNew(i)
  • end

40
xNew(1) (x(1)x(2))/2yNew(1) (y(1)y(2))/2
(x2,y2)
(x1,y1)
(x5,y5)
(x3,y3)
(x4,y4)
41
xNew(2) (x(2)x(3))/2yNew(2) (y(2)y(3))/2
(x2,y2)
(x1,y1)
(x5,y5)
(x3,y3)
(x4,y4)
42
xNew(3) (x(3)x(4))/2yNew(3) (y(3)y(4))/2
(x2,y2)
(x1,y1)
(x5,y5)
(x3,y3)
(x4,y4)
43
xNew(4) (x(4)x(5))/2yNew(4) (y(4)y(5))/2
(x2,y2)
(x1,y1)
(x5,y5)
(x3,y3)
(x4,y4)
44
xNew(5) (x(5)x(1))/2yNew(5) (y(5)y(1))/2
(x2,y2)
(x1,y1)
(x5,y5)
(x3,y3)
(x4,y4)
45
xNew(5) (x(5)x(1))/2yNew(5) (y(5)y(1))/2
(x2,y2)
(x1,y1)
(x5,y5)
(x3,y3)
(x4,y4)
46
Smooth
  • for i1n
  • xNew(i) (x(i) x(i1))/2
  • yNew(i) (y(i) y(i1))/2
  • end
  • Will result in a subscript
  • out of bounds error when i is n.

47
Smooth
  • for i1n
  • if iltn
  • xNew(i) (x(i) x(i1))/2
  • yNew(i) (y(i) y(i1))/2
  • else
  • xNew(n) (x(n) x(1))/2
  • yNew(n) (y(n) y(1))/2
  • end
  • end

48
Smooth
  • for i1n-1
  • xNew(i) (x(i) x(i1))/2
  • yNew(i) (y(i) y(i1))/2
  • end
  • xNew(n) (x(n) x(1))/2
  • yNew(n) (y(n) y(1))/2

49
Proposed Simulation
  • Create a polygon with randomly
  • located vertices.
  • Repeat
  • Centralize
  • Normalize
  • Smooth

50
(No Transcript)
51
2D Random Walk
52
Start in middle tile. Repeat until boundary
reached Pick a compass heading at
random. Move one tile in that direction.
1-by-1 tiles
North, East, South, West
53
Function that Returns the Path
function x y RandomWalk2D(N) k 0 xc
0 yc 0 while abs(xc)ltN abs(yc)lt N
Take another hop. Update location
(xc,yc). k k 1 x(k) xc y(k) yc
end
54
  • k x(k) y(k)
  • -------------
  • 1 0 -1
  • 2 0 0
  • 3 -1 0
  • 4 0 0
  • 5 0 1
  • 6 1 1
  • 7 1 0
  • 8 1 1
  • 0 1

55
  • if rand lt .5
  • if rand lt .5
  • xc xc 1 East
  • else
  • xc xc - 1 West
  • end
  • else
  • if rand lt .5
  • yc yc 1 North
  • else
  • yc yc - 1 Sounth
  • end
  • end

56
How Many Returns to (0,0)?
  • x and y are n-vectors.
  • How many times do we have
  • (x(k),y(k)) (0,0)?
  • m 0
  • for k1length(x)
  • if x(k)0 y(k)0
  • m m 1
  • end
  • end
Write a Comment
User Comments (0)
About PowerShow.com