L 14 Arrays and Functions with array parameters

  • Slides: 56
Download presentation
L 14. Arrays and Functions with array parameters. Row and column vectors Built-Ins: length,

L 14. Arrays and Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand, randn, max

Row and Column Vectors >> v = [1 2 3] v= 1 2 3

Row and Column Vectors >> v = [1 2 3] v= 1 2 3 >> v = [1 ; 2 ; 3] v= Observe 1 semicolons 2 3

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

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

rand( , ) >> x = rand(3, 1) x = 0. 2618 0. 7085

rand( , ) >> x = rand(3, 1) x = 0. 2618 0. 7085 0. 7839 >> x = rand(1, 3) x = 0. 9862 0. 4733 0. 9028

randn( , • >> x = randn(1, 3) • x = • 0. 2877

randn( , • >> x = randn(1, 3) • x = • 0. 2877 -1. 1465 • >> x = randn(3, 1) • x = • 1. 1892 • -0. 0376 • 0. 3273 ) 1. 1909

Normal Distribution with Zero Mean and Unit STD

Normal Distribution with Zero Mean and Unit STD

Affirmations >> n = 1000000; >> x = randn(n, 1); >> ave = sum(x)/n

Affirmations >> n = 1000000; >> x = randn(n, 1); >> ave = sum(x)/n ave = -0. 0017 >> stand. Dev = std(x) stand. Dev = 0. 9989

length >> v = randn(1, 5); >> n = length(v) n = 5 >>

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.

Augmenting Row Vectors >> x = [10 20] x = 10 20 >> x

Augmenting Row Vectors >> x = [10 20] x = 10 20 >> x = [x 30] x = 10 >> 20 30

Augmenting Column Vectors >> x = [10; 20] x = 10 20 >> x

Augmenting Column Vectors >> x = [10; 20] x = 10 20 >> x = [x ; 30] x = 10 20 30 Observe semicolons!

“Concatenating” Row Vectors >> x = [10 20] x = 10 20 >> y

“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

“Concatenating” Column Vectors >> x = [10 ; 20]; >> y = [30 ;

“Concatenating” Column Vectors >> x = [10 ; 20]; >> y = [30 ; 40 ; 50]; >> z = [ x ; y ] z = 10 20 30 Observe 40 semicolons! 50

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

Application Plot sine across [0, 4*pi] and use the fact that it has period 2 pi. x = linspace(0, 2*pi, 100); y = sin(x); x = [x x+2*pi]; y = [y y]; plot(x, y)

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

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

The Empty Vector x = [ ]; for k=1: 50 if floor(sqrt(k))==sqrt(k) x =

The Empty Vector x = [ ]; for k=1: 50 if floor(sqrt(k))==sqrt(k) x = [x; k]; end x = x 1 4 9 16 25 36 49

Array Hints & Errors

Array Hints & Errors

Dimension Mismatch >> x = [1; 2] x = 1 2 >> y =

Dimension Mismatch >> x = [1; 2] x = 1 2 >> y = [3 4] y = 3 4 Can’t add a row vector to a column vector >> z = x+y ? ? ? Error using ==> plus Matrix dimensions must agree.

Not a Syntax Error >> x = rand(3) x = 0. 9501 0. 4860

Not a Syntax Error >> x = rand(3) x = 0. 9501 0. 4860 0. 2311 0. 8913 0. 6068 0. 7621 0. 4565 0. 0185 0. 8214 You probably meant to say x = rand(1, 3) or x = rand(3, 1).

A Style Hint Assume n is initialized. a = zeros(1, n) a = [

A Style Hint Assume n is initialized. a = zeros(1, n) a = [ ]; for k=1: n a(k) = sqrt(k); end a = [a sqrt(k)]; end Better because it reminds you of the size and shape of the array you set up.

Error: Out-of. Range Subscript >> x = [10 20 30] x = 10 20

Error: Out-of. Range Subscript >> x = [10 20 30] x = 10 20 30 >> c = x(4) ? ? ? Index exceeds matrix dimensions.

This is OK… >> x = [ 10 20 30] x = 10 20

This is OK… >> x = [ 10 20 30] x = 10 20 30 >> x(4) = 100 x = 10 20 30 100

Forgot the Semicolon? >> x = randn(1000000, 1)

Forgot the Semicolon? >> x = randn(1000000, 1)

Forgot the Semicolon? >> x = randn(1000000, 1) Remember: ctrl-C

Forgot the Semicolon? >> x = randn(1000000, 1) Remember: ctrl-C

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

Question Time A = [ 1 ]; while(length(A) < 5) A = [length(A)+1 ; A]; end A = A Is this the same as A = linspace(1, 5, 5) A. Yes B. No ?

No! Linspace: 1 2 3 Fragment: 5 4 3 2 1 4 5

No! Linspace: 1 2 3 Fragment: 5 4 3 2 1 4 5

Question Time x = zeros(1, 1); for k=1: 3 x = [x x]; end

Question Time x = zeros(1, 1); for k=1: 3 x = [x x]; end y = x(7) Will this cause a subscript out of bounds error? A. Yes B. No

No! How x changes: After 1 st pass: [0 0] After 2 nd pass:

No! How x changes: After 1 st pass: [0 0] After 2 nd pass: [ 0 0 ] After 3 rd pass: [0 0 0 0 0] So y = x(7) makes sense.

Polygon Transformations Functions & arrays

Polygon Transformations Functions & arrays

A Polygon (x 2, y 2) (x 1, y 1) Store xycoordinates in vectors

A Polygon (x 2, y 2) (x 1, y 1) Store xycoordinates in vectors x and y. (x 5, y 5) (x 3, y 3) (x 4, y 4)

Operation 1: Centralize Move a polygon so that the centroid of its vertices is

Operation 1: Centralize Move a polygon so that the centroid of its vertices is at the origin.

Centralize Before After

Centralize Before After

Centralize function [x. New, y. New] = Centralize(x, y) n = length(x); x. Bar

Centralize function [x. New, y. New] = Centralize(x, y) n = length(x); x. Bar = sum(x)/n; Computes y. Bar = sum(y)/n; the vertices of the new x. New = x-x. Bar; polygon y. New = y-y. Bar; Notice how length is used to figure out the size of the incoming vectors.

Operation 2: Normalize Shrink (enlarge) the polygon so that the vertex furthest from the

Operation 2: Normalize Shrink (enlarge) the polygon so that the vertex furthest from the (0, 0) is on the unit circle.

Normalize Before After

Normalize Before After

Normalize function [x. New, y. New] = Normalize(x, y) d = max(sqrt(x. ^2 +

Normalize function [x. New, y. New] = Normalize(x, y) d = max(sqrt(x. ^2 + y. ^2)); x. New = x/d; y. New = y/d; Applied to a vector, max returns the largest value in the vector.

Operation 3: Smooth Obtain a new polygon by connecting the midpoints of the edges

Operation 3: Smooth Obtain a new polygon by connecting the midpoints of the edges

Smooth

Smooth

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

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

Smooth function [x. New, y. New] = Smooth(x, y) n = length(x); x. New

Smooth function [x. New, y. New] = Smooth(x, y) n = length(x); x. New = zeros(n, 1); y. New = zeros(n, 1); for i=1: n Compute the mdpt of ith edge. Store in x. New(i) and y. New(i) end

x. New(1) = (x(1)+x(2))/2 y. New(1) = (y(1)+y(2))/2 (x 2, y 2) (x 1,

x. New(1) = (x(1)+x(2))/2 y. New(1) = (y(1)+y(2))/2 (x 2, y 2) (x 1, y 1) (x 5, y 5) (x 3, y 3) (x 4, y 4)

x. New(2) = (x(2)+x(3))/2 y. New(2) = (y(2)+y(3))/2 (x 2, y 2) (x 1,

x. New(2) = (x(2)+x(3))/2 y. New(2) = (y(2)+y(3))/2 (x 2, y 2) (x 1, y 1) (x 5, y 5) (x 3, y 3) (x 4, y 4)

x. New(3) = (x(3)+x(4))/2 y. New(3) = (y(3)+y(4))/2 (x 2, y 2) (x 1,

x. New(3) = (x(3)+x(4))/2 y. New(3) = (y(3)+y(4))/2 (x 2, y 2) (x 1, y 1) (x 5, y 5) (x 3, y 3) (x 4, y 4)

x. New(4) = (x(4)+x(5))/2 y. New(4) = (y(4)+y(5))/2 (x 2, y 2) (x 1,

x. New(4) = (x(4)+x(5))/2 y. New(4) = (y(4)+y(5))/2 (x 2, y 2) (x 1, y 1) (x 5, y 5) (x 3, y 3) (x 4, y 4)

x. New(5) = (x(5)+x(1))/2 y. New(5) = (y(5)+y(1))/2 (x 2, y 2) (x 1,

x. New(5) = (x(5)+x(1))/2 y. New(5) = (y(5)+y(1))/2 (x 2, y 2) (x 1, y 1) (x 5, y 5) (x 3, y 3) (x 4, y 4)

x. New(5) = (x(5)+x(1))/2 y. New(5) = (y(5)+y(1))/2 (x 2, y 2) (x 1,

x. New(5) = (x(5)+x(1))/2 y. New(5) = (y(5)+y(1))/2 (x 2, y 2) (x 1, y 1) (x 5, y 5) (x 3, y 3) (x 4, y 4)

Smooth for i=1: n x. New(i) = (x(i) + x(i+1))/2; y. New(i) = (y(i)

Smooth for i=1: n x. New(i) = (x(i) + x(i+1))/2; y. New(i) = (y(i) + y(i+1))/2; end Will result in a subscript out of bounds error when i is n.

Smooth for i=1: n if i<n x. New(i) y. New(i) else x. New(n) y.

Smooth for i=1: n if i<n x. New(i) y. New(i) else x. New(n) y. New(n) end = (x(i) + x(i+1))/2; = (y(i) + y(i+1))/2; = (x(n) + x(1))/2; = (y(n) + y(1))/2;

Smooth for i=1: n-1 x. New(i) = (x(i) + x(i+1))/2; y. New(i) = (y(i)

Smooth for i=1: n-1 x. New(i) = (x(i) + x(i+1))/2; y. New(i) = (y(i) + y(i+1))/2; end x. New(n) = (x(n) + x(1))/2; y. New(n) = (y(n) + y(1))/2;

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

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

2 D Random Walk

2 D Random Walk

Start in middle tile. Repeat until boundary reached: Pick a compass heading* at random.

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

Function that Returns the Path function [x y] = Random. Walk 2 D(N) k

Function that Returns the Path function [x y] = Random. Walk 2 D(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

k x(k) y(k) ------1 0 -1 2 0 0 3 -1 0 4 0

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 9 0 1 : : :

if rand <. 5 if rand xc = else xc = end else if

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

How Many Returns to (0, 0)? % x and y are n-vectors. % How

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; end