16. two-dimensional arrays · rows and columns 12 17 49 61 38 18 82 77 83 53 12 10 a: a is a 3-by-4...

43
16. Two-Dimensional Arrays Set-Up Rows and Columns Subscripting Operations Examples

Upload: others

Post on 17-May-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

16. Two-Dimensional Arrays

Set-UpRows and ColumnsSubscriptingOperationsExamples

Page 2: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Simple Set-Up Examples

>> A = [1 2 3; 4 5 6]A =

1 2 34 5 6

Page 3: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Simple Set-Up Examples

>> A = zeros(3,4)A =

0 0 0 00 0 0 00 0 0 0

Page 4: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Simple Set-Up Examples

>> A = floor(100*rand(5,5))A =

95 76 61 40 523 45 79 93 3560 1 92 91 8148 82 73 41 089 44 17 89 13

Page 5: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Simple Set-Up Examples

>> A = [zeros(3,2) [1;2;3]]A =

0 0 10 0 20 0 3

Page 6: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Simple Set-Up Examples

>> A = [zeros(3,2) ; [1 2] ]A =

0 0 0 0 0 0 1 2

Page 7: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Rows and Columns

12 17 49 61

38 18 82 77

83 53 12 10

A:

A is a 3-by-4 array: 3 rows 4 columns.

row 1

row 2

row 3

col4

col3

col2

col1

Page 8: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Subscripting

12 17 49 61

38 18 82 77

83 53 12 10

A:

Individual entries: A(3,2)

Page 9: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Subscripting

12 17 49 61

38 18 82 77

83 53 12 10

A:

An Entire Row: A(2,:)

Page 10: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Scaling a Row

12 17 49 61

10 20 30 40

83 53 12 10

A:

A(2,:) = 10*A(2,:)

12 17 49 61

1 2 3 4

83 53 12 10

A:

Before After

Page 11: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Subscripting

12 17 49 61

38 18 82 77

83 53 12 10

A:

An Entire Column: A(:,3)

Page 12: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Incrementing the Values in a Column

12 17 49 61

38 18 82 77

83 53 12 10

A:

A(:,3) = A(:,3) + 1

12 17 50 61

38 18 83 77

83 53 13 10

A:

Before After

Page 13: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Subscripting

12 17 49 61

38 18 82 77

83 53 12 10

A:

A General Subarray: A(2:3,3:4)

Page 14: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Zeroing a Subarray

12 17 49 61

38 18 82 77

83 53 12 10

A:

A(2:3,3:4) = zeros(2,2)

12 17 49 61

38 18 0 0

83 53 0 0

A:

Before After

Page 15: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Classical Double Loop Set-Up11 21 31 41

12 22 32 42

13 23 33 43

A:

for i=1:3

for j=1:4

A(i,j) = 10*j + i;

end

end

Page 16: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Set-Up By Row11 21 31 41

12 22 32 42

13 23 33 43

A:

A = [];

for i=1:3

v = [10 20 30 40] + i;

A = [A ; v]

end

Page 17: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Set-Up By Column11 21 31 41

12 22 32 42

13 23 33 43

A:

A = [];

for j=1:4

v = 10*j + [1;2;3];

A = [A v]

end

Page 18: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Question TimeA = [ 1 2 3; 4 5 6];C = A(:,2);

What the value of A(2,2)?

A. 4 B. 5 C. 6

Page 19: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Question TimeA = [ 1 2 3; 4 5 6];A = A(1:2,2:3)

What the value of A(2,2)?

A. 4 B. 5 C. 6

Page 20: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Largest Value

12 17 49 61

38 18 82 77

83 53 12 10

A:

m = max(A) ; M = max(m)

83 53 82 77m: M: 83

Page 21: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Functions and 2D Arrays

function alpha = Ave(A)% A is a 2D array.% alpha is the average of its% values.

10 20 3040 50 60

-> (10+20+30+40+50+60)/6

Page 22: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Need Built-In Function size

function alpha = Ave(A)[m,n] = size(A);

Add up all the numbers inthe array. Store in s.

alpha = s/(m*n);

size(A) returns #rows and # columns

Page 23: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Refine…function alpha = Ave(A)

[m,n] = size(A);s = 0;for i=1:m

sRow = the sum of the values in A(i,:)s = s + sRow

endalpha = s/(m*n);

Page 24: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

sRow = the sum of thevalues in A(i,:)

sRow = 0;for j=1:n

sRow = sRow + A(i,j);end

Page 25: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

function alpha = Ave(A)[m,n] = size(A);s = 0;for i=1:m

s = s + sRowendalpha = s/(m*n);

sRow = 0;for j=1:n

sRow = sRow + A(i,j);end

Page 26: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Now Some More Involved Examples

Page 27: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Random WebN web pages

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.

Page 28: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Random Link Idea

A(i,,j) = 1 with probability

More likely to be a link if i is close to j.

||11

ji−+

Page 29: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

function A = RandomLinks(n) A = zeros(n,n);for i=1:nfor j=1:nr = rand;if i~=j && r<= 1/(1 + abs(i-j));

A(i,j) = 1;end

endend

Page 30: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

0 1 1 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 01 0 0 0 1 0 0 0 1 1 1 0 0 0 0 0 0 1 0 00 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 00 1 1 1 1 1 0 0 0 1 0 1 1 0 0 0 0 0 0 00 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 10 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0 0 00 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 10 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 0 00 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 10 0 0 1 0 0 0 0 1 1 0 1 0 1 1 0 0 0 0 00 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 00 0 0 0 0 1 0 1 0 0 0 0 1 0 0 1 0 0 0 10 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 1 00 1 0 0 0 0 0 0 1 0 0 0 0 1 0 1 0 1 1 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 10 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0

N = 20

Page 31: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

100 Web pages. Now display the links….

Page 32: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Line black as it leaves page j, red when it arrives at page i.

Page 33: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

A Cost/Inventory ProblemA company has 3 factories that make 5

different products.The cost of making a product varies from

factory to factory.The inventory varies from factory to

factory.A customer submits a purchase order that is

to be filled by a single factory.

Find the cheapest way to do this.

Page 34: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Cost Array

C:

10 36 22 15

12 35 20 12

13 37 21 16

66

62

59

The value of C(i,j) is what it costs factory i to make product j.

Page 35: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Inventory Array

38 5 99 34

82 19 83 12Inv:

51 29 21 56

42

42

87

The value of Inv(i,j) is the inventory in factory i of product j.

Page 36: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

Purchase Order

The value of PO(j) is the numberproduct j’s that the customer wants

1 0 12 529PO:

Page 37: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

How Much Does it Cost for Each Factory to Process

a Purchase order?

Page 38: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

1 0 12 529PO:

C:

10 36 22 15

12 35 20 12

13 37 21 16

66

62

59

For factory 1:

1*10 + 0*36 + 12*22 + 29* 15 + 5*62

Page 39: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

1 0 12 529PO:

C:

10 36 22 15

12 35 20 12

13 37 21 16

66

62

59

s = 0;for j=1:5

s = s + C(1,j)*PO(j)end

For factory 1:

Page 40: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

1 0 12 529PO:

C:

10 36 22 15

12 35 20 12

13 37 21 16

66

62

59

s = 0;for j=1:5

s = s + C(2,j)*PO(j)end

For factory 2:

Page 41: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

1 0 12 529PO:

C:

10 36 22 15

12 35 20 12

13 37 21 16

66

62

59

s = 0;for j=1:5

s = s + C(i,j)*PO(j)end

For factory i:

Page 42: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

function TheBill = iCost(i,C,PO)

nProd = length(PO)TheBill = 0;for j=1:nProd

TheBill = TheBill + C(I,j)*PO(j);end

Encapsulate…

Page 43: 16. Two-Dimensional Arrays · Rows and Columns 12 17 49 61 38 18 82 77 83 53 12 10 A: A is a 3-by-4 array: 3 rows 4 columns. row 1 row 2 row 3 col 4 col 3 col 2 col 1

function [iStar,B] = Cheapest(C,PO)[nFact,nProd] = size(C)iStar=1; B = iCost(1,C,PO);for i=2:nFact

iB = iCost(i,C,PO);if iB < B

iStar = I; B = iB;end

end

The Cheapest?