+ All Categories
Home > Documents > Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column...

Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column...

Date post: 19-Dec-2015
Category:
View: 215 times
Download: 2 times
Share this document with a friend
56
Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand, randn, max
Transcript
Page 1: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

14. Still More on Arrays

Functions with array parameters.

Row and column vectors

Built-Ins: length, zeros, stdRevisit: rand, randn, max

Page 2: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

Row and Column Vectors

>> v = [1 2 3]v = 1 2 3

>> v = [1 ; 2 ; 3]v = 1 2 3

Observe semicolons

Page 3: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

zeros( , )

>> x = zeros(3,1)x = 0 0 0>> x = zeros(1,3)x = 0 0 0

Page 4: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

rand( , )

>> x = rand(3,1)x = 0.2618 0.7085 0.7839

>> x = rand(1,3)x = 0.9862 0.4733 0.9028

Page 5: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

randn( , )

>> x = randn(1,3)

x =

0.2877 -1.1465 1.1909

>> x = randn(3,1)

x =

1.1892

-0.0376

0.3273

Page 6: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

Normal Distribution withZero Mean and Unit STD

Page 7: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

Affirmations

>> n = 1000000;>> x = randn(n,1);

>> ave = sum(x)/nave = -0.0017

>> standDev = std(x)standDev = 0.9989

Page 8: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

length

>> v = randn(1,5);

>> n = length(v)

n =

5

>> u = rand(5,1);

>> n = length(u)

n =

5

The length function doesn’t care about row or column orientation.

Page 9: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

Augmenting Row Vectors

>> x = [10 20]

x =

10 20

>> x = [x 30]

x =

10 20 30

>>

Page 10: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

Augmenting Column Vectors

>> x = [10;20]

x =

10

20

>> x = [x ; 30]

x =

10

20

30Observe semicolons!

Page 11: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

“Concatenating” Row Vectors

>> x = [10 20]

x =

10 20

>> y = [30 40 50]

y =

30 40 50

>> z = [x y]

z =

10 20 30 40 50

Page 12: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

“Concatenating” Column Vectors

>> x = [10 ; 20];

>> y = [30 ; 40 ; 50];

>> z = [ x ; y ]

z =

10

20

30

40

50Observe semicolons!

Page 13: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

Application

x = linspace(0,2*pi,100);

y = sin(x);

x = [x x+2*pi];

y = [y y];

plot(x,y)

Plot sine across [0,4*pi] and use the fact that it has period 2pi.

Page 14: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

x = linspace(0,2*pi,100);x = [ x x+2*pi ];

linspace(2*pi,4*pi,100)]

Page 15: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

The Empty Vector

x = [ ]; for k=1:50 if floor(sqrt(k))==sqrt(k) x = [x; k]; endendx = x

x = 1 4 9 16 25 36 49

Page 16: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

Array Hints & Errors

Page 17: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

Dimension Mismatch

>> x = [1;2]x = 1 2

>> y = [3 4]y = 3 4

>> z = x+y??? Error using ==> plus Matrix dimensions must agree.

Can’t add a row vector to a column vector

Page 18: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

Not a Syntax Error

>> 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 sayx = rand(1,3) or x = rand(3,1).

Page 19: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

A Style Hint

Assume n is initialized.

a = zeros(1,n)

for k=1:n

a(k) = sqrt(k);

end

a = [ ];

for k=1:n

a = [a sqrt(k)];

end

Better because it reminds you of the size and shape of the array you set up.

Page 20: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

Error: Out-ofRange Subscript

>> x = [10 20 30]

x = 10 20 30

>> c = x(4)

??? Index exceeds matrix dimensions.

Page 21: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

This is OK…

>> x = [ 10 20 30]x = 10 20 30

>> x(4) = 100x = 10 20 30 100

Page 22: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

Forgot the Semicolon?

>> x = randn(1000000,1)

Page 23: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

Forgot the Semicolon?

>> x = randn(1000000,1)

Remember: ctrl-C

Page 24: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

Question Time

A = [ 1 ];while(length(A) < 5) A = [length(A)+1 ; A];endA = A

Is this the same as

A = linspace(1,5,5) ?

A. Yes B. No

Page 25: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

No!

Linspace: 1 2 3 4 5

Fragment: 5 4 3 2 1

Page 26: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

Question Time

x = zeros(1,1);for k=1:3 x = [x x];endy = x(7)

Will this cause a subscript out ofbounds error?

A. Yes B. No

Page 27: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

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.

Page 28: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

Polygon Transformations

Functions & arrays

Page 29: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

A Polygon

(x4,y4)

(x1,y1)

(x3,y3)

(x2,y2)

(x5,y5)

Store xy-coordinates in vectors x and y.

Page 30: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

Operation 1: Centralize

Move a polygon so that the centroidof its vertices is at the origin.

Page 31: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

Before

After

Centralize

Page 32: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

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.

Page 33: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

Operation 2: Normalize

Shrink (enlarge) the polygon so thatthe vertex furthest from the(0,0) is on the unit circle.

Page 34: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

Before

After

Normalize

Page 35: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

function [xNew,yNew] = Normalize(x,y)

d = max(sqrt(x.^2 + y.^2));

xNew = x/d;

yNew = y/d;

Normalize

Applied to a vector, max returns the largest value in the vector.

Page 36: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

Operation 3: Smooth

Obtain a new polygon by connectingthe midpoints of the edges

Page 37: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

Smooth

Page 38: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

Midpoints

(a,b)

( (a+c)/2 , (b+d)/2 )

(c,d)

Page 39: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

function [xNew,yNew] = Smooth(x,y) n = length(x); xNew = zeros(n,1); yNew = zeros(n,1);

for i=1:n Compute the mdpt of ith edge. Store in xNew(i) and yNew(i) end

Smooth

Page 40: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing (x4,y4)

(x1,y1)

(x3,y3)

(x2,y2)

(x5,y5)

xNew(1) = (x(1)+x(2))/2yNew(1) = (y(1)+y(2))/2

Page 41: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing (x4,y4)

(x1,y1)

(x3,y3)

(x2,y2)

(x5,y5)

xNew(2) = (x(2)+x(3))/2yNew(2) = (y(2)+y(3))/2

Page 42: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing (x4,y4)

(x1,y1)

(x3,y3)

(x2,y2)

(x5,y5)

xNew(3) = (x(3)+x(4))/2yNew(3) = (y(3)+y(4))/2

Page 43: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing (x4,y4)

(x1,y1)

(x3,y3)

(x2,y2)

(x5,y5)

xNew(4) = (x(4)+x(5))/2yNew(4) = (y(4)+y(5))/2

Page 44: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing (x4,y4)

(x1,y1)

(x3,y3)

(x2,y2)

(x5,y5)

xNew(5) = (x(5)+x(1))/2yNew(5) = (y(5)+y(1))/2

Page 45: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing (x4,y4)

(x1,y1)

(x3,y3)

(x2,y2)

(x5,y5)

xNew(5) = (x(5)+x(1))/2yNew(5) = (y(5)+y(1))/2

Page 46: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

for i=1:n xNew(i) = (x(i) + x(i+1))/2; yNew(i) = (y(i) + y(i+1))/2;end

Will result in a subscriptout of bounds error when i is n.

Smooth

Page 47: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

for i=1:n if i<n xNew(i) = (x(i) + x(i+1))/2; yNew(i) = (y(i) + y(i+1))/2; else xNew(n) = (x(n) + x(1))/2; yNew(n) = (y(n) + y(1))/2; endend

Smooth

Page 48: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

for i=1:n-1

xNew(i) = (x(i) + x(i+1))/2;

yNew(i) = (y(i) + y(i+1))/2;

end

xNew(n) = (x(n) + x(1))/2;

yNew(n) = (y(n) + y(1))/2;

Smooth

Page 49: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

Proposed Simulation

Create a polygon with randomlylocated vertices.

Repeat: Centralize Normalize Smooth

Page 50: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

Page 51: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

2D Random Walk

Page 52: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

Start in middle tile.

Repeat untilboundary reached:

Pick a compass heading* at random.

Move one tile in that direction.

* North, East, South, West1-by-1 tiles

Page 53: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

function [x y] = RandomWalk2D(N) k = 0; xc = 0; yc = 0; while abs(xc)<N && abs(yc)< N

Take another hop. Update location (xc,yc).

k = k + 1; x(k) = xc; y(k) = yc; end

Function that Returns the Path

Page 54: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

k x(k) y(k)-------------1 0 -12 0 03 -1 04 0 05 0 16 1 17 1 08 1 19 0 1: : :

Page 55: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

if rand < .5 if rand < .5 xc = xc + 1; % East else xc = xc - 1; % West end else if rand < .5 yc = yc + 1; % North else yc = yc - 1; % Sounth end end

Page 56: Insight Through Computing 14. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand,

Insight Through Computing

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 k=1:length(x) if x(k)==0 && y(k)==0 m = m + 1; endend


Recommended