Professional Documents
Culture Documents
Previous Lecture:
Vectors Color computation Linear interpolation 2-d arraymatrix Discussion in classrooms as listed in Student Center Optional review sessions T 4:40 PHS101, W 5:30 UPS B7 Prelim 1 Thursday at 7:30pm
Initialize vectors/matrices if dimensions are known instead of building the array one component at a time
% Initialize y x=linspace(a,b,n); y=zeros(1,n); for k=1:n x(k) y(k)=myF(x); end % Build y on the fly x=linspace(a,b,n); for k=1:n x(k) y(k)=myF(x); end
- run time: worse because there are extra copying per each iteration - copies the previous elements in the vector - when you're working with vectors, when you13know how long your vector is going Lecture 20 to be, initialize the vector with "y=zeros(1,n)"... ::: bucket example
Todays Lecture:
Announcements:
10
36 35 37
22 20 21
15 12 16
62 66 59
12 13
An array is a named collection of like data organized into rows and columns A 2-d array is a table, called a matrix Two indices identify the position of a value in a matrix, e.g., mat(r,c) refers to component in row r, column c of matrix mat Array index starts at 1 Rectangular: all rows have the same #of columns
Lecture 13 22
Lecture 13
Creating a matrix
2 3 5
-1 .5 8 6
0 7 7
-3 7 2
Given a matrix M
[nr, nc]= size(M) nr= size(M, 1) nc= size(M, 2) M(2,4)= 1; disp(M(3,1)) M(1,nc)= 4;
23
-3 8.5 9 10
Build a matrix using square brackets, [ ], but the dimension must match up:
52 81 .5
[x y] puts y to the right of x [x; y] puts y below x [4 0 3; 5 1 9] creates the matrix ones(row,colums) [4 0 3; ones(1,3)] gives 4 1 [4 0 3; ones(3,1)] doesnt work
M(row2,column4) is 1
M(first row, last column) nc is seen as the last column of the array
Lecture 13 24
Lecture 13
Lecture slides
CS1112 Lecture 13
A= [0 0] A= [A ones(2,1)] A= [0 0 0 0; A
1. A 2. A 3. A 0 0 0 0 0 0 0 1 1 0 1 1
A]
B C
0 0 0
0 1 1
Example: minimum value in a matrix function val = minInMatrix(M) % val is the smallest value in matrix M
1 2 .. r ..
Pattern for traversing a matrix M [nr, nc] = size(M) for r= 1:nr executing nr * nc times % At row r for c= 1:nc % At column c (in row r) % % Do something with M(r,c) end end
Lecture 13 30
[nr, nc] = size(M) val = M(1,1); %you will not use 0 because you can have negative #s for r = 1:nr %at row index r for c = 1:nc %at column c (at a row r) if M(r,c) < val val = M(r,c); end end end
Lecture 13
28
N web pages can be represented by an N-by-N Link Array A. A(i,j) is 1 if there is a link on webpage j to webpage i Generate a random link array and display the connectivity: There is no link from a page to itself 1 If ij then A(i,j) = 1 with probability 1|i j|
function A = RandomLinks(n) % A is n-by-n matrix of 1s and 0s % representing n webpages A = zeros(n,n); for i=1:n for j=1:n r = rand(1); r = random number from 0 to 1 if i~=j && r<= 1/(1 + abs(i-j)); A(i,j) = 1; (probability) end end end
Lecture 13 32
first condition: in the diagonal of the square shape array, the diagonal contains 0s Lecture 13 31 second condition: near the diagonal, there are more 1s than on the corners / edges
Lecture slides
CS1112 Lecture 13
Random web N = 20
01110000010010000000 10001000111000000100 01010000000000000000 00101000000000000000 00010000001100000000 00000000000001010000 01111100010110000000 00000010000100000011 01000000010010001000 00000001101000000001 00000010000011000000 00000010010000000001 00010000110101100000 00000010000000110000 00000101000010010001 00000010001000001010 01000000100001010110 00000000000000011001 00000010000000000000 00000000000000001010
Lecture 13 33
ShowRandomLinks.m
for i = 1:n for j = 1:n if A(i,j) = = 1 && A(j,i) == 1 %blue elseif A(i,j) == 1 %black-red % j--> mid = black; mid-->i = red %overriding the blue line end end end
Bidirectional links are blue. Unidirectional link is black as it leaves page j, red when it arrives at page i.
35
Lecture 13
36
% Given an nr-by-nc matrix M. % What is A? for r= 1: nr for c= 1: nc A(c,r)= M(r,c); end end A A is M with the columns in reverse order B A is M with the rows in reverse order b. C A is the transpose of M c. D A and M are the same d.
a.
Lecture 13 37
A Cost/Inventory Problem
A company has 3 factories that make 5 different products The cost of making a product varies from factory to factory The inventory/capacity varies from factory to factory
Lecture 13
41
Lecture slides
CS1112 Lecture 13
Cost Array
10
36 35 37
22 20 21
15 12 16
62 66 59
1. How much would it cost a factory to fill the order? 2. Does a factory have enough inventory/capacity to fill the order? 3. Among the factories that can fill the order, who can do it most cheaply?
Lecture 13 42
12 13
Purchase Order
38
5 19 29
99 83 21
34 12 56
42 42 87
Inv
82 51
PO
12 29 5
The value of PO(j) is the number of product js that the customer wants
Lecture 13
45
10
36 35 37
22 20 21
15 12 16
62 66 59
Encapsulate
function TheBill = iCost(i,C,PO) % The cost when factory i fills the % purchase order nProd = length(PO); TheBill = 0; for j=1:nProd TheBill = TheBill + C(i,j)*PO(j); end
12 13
12 29 5
Lecture 13
50
Lecture slides
CS1112 Lecture 13
10
36 35 37
22 20 21
15 12 16
62 66 59
12 13
PO
12 29 5
Lecture 13
51
Lecture 13
52
inf a special value that can be regarded as positive infinity x y z w = = = < 10/0 1+x 1/x inf assigns inf to x assigns inf to y assigns 0 to z is always true if w is numeric
Inventory/Capacity Considerations What if a factory lacks the inventory/capacity to fill the purchase order? Such a factory should be excluded from the findthe-cheapest computation.
Lecture 13
53
Lecture 13
54
Wanted: A True/False Function Yes No Yes i Inv PO DO is true if factory i can fill the order. DO is false if factory i cannot fill the order.
Lecture 13 55 Lecture 13 56
Inv
82 51
iCanDo
DO
PO
12 29 5
Lecture slides
CS1112 Lecture 13
Encapsulate
function DO = iCanDo(i,Inv,PO) % DO is true if factory i can fill % the purchase order. Otherwise, false nProd = length(PO); DO = 1; for j = 1:nProd DO = DO && ( Inv(i,j) >= PO(j) ); end
57 Lecture 13 64
38
5 19 29
99 83 21
34 12 56
42 42 87
Inv
82 51
PO
12 29 5
Lecture 13
Encapsulate
function DO = iCanDo(i,Inv,PO) % DO is true if factory i can fill % the purchase order. Otherwise, false nProd = length(PO); j = 1; while j<=nProd && Inv(i,j)>=PO(j) j = j+1; DO should be true when end A j < nProd B j == nProd DO = _______; C j > nProd
Lecture 13 66
Lecture 13
68
Yes No Yes
12 13
PO
12 29 5
As computed by iCost
Lecture 13
As computed by iCanDo
72
Lecture slides