{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS 100 M Lecture 12

# CS 100 M Lecture 12 - 12 Still More on Arrays Functions...

This preview shows pages 1–17. Sign up to view the full content.

12. Still More on Arrays Functions with array parameters. Row and column vectors Built-Ins: length, zeros, std Revisit: rand, randn, max

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Row and Column Vectors >> v = [1 2 3] v = 1 2 3 >> v = [1 ; 2 ; 3] v = 1 2 3 Observe semicolons
zeros( , ) >> x = zeros(3,1) x = 0 0 0 >> x = zeros(1,3) x = 0 0 0

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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 -1.1465 1.1909 >> x = randn(3,1) x = 1.1892 -0.0376 0.3273

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Normal Distribution with Zero Mean and Unit STD -3 -2 -1 0 1 2 3 0 10000 20000 30000 40000 50000 60000 70000 80000 90000 100000 110000 Distribution of randn(1000000,1)
Affirmations >> n = 1000000; >> x = randn(n,1); >> ave = sum(x)/n ave = -0.0017 >> standDev = std(x) standDev = 0.9989

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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 = [x 30] x = 10 20 30 >>

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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 = [30 40 50] y = 30 40 50 >> z = [x y] z = 10 20 30 40 50

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
“Concatenating” Column Vectors >> x = [10 ; 20]; >> y = [30 ; 40 ; 50]; >> z = [ x ; y ] z = 10 20 30 40 50 Observe semicolons!
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.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
0 2 4 6 8 10 12 14 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 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 = [x; k]; end end x = x x = 1 4 9 16 25 36 49

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Array Hints & Errors
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}