17 Structures Simple Structures Structure Arrays Structures with

  • Slides: 41
Download presentation
17. Structures Simple Structures Structure Arrays Structures with Array Fields Other Possibilities

17. Structures Simple Structures Structure Arrays Structures with Array Fields Other Possibilities

Array vs. Cell Array n Simple array n n n Each component stores one

Array vs. Cell Array n Simple array n n n Each component stores one value (e. g. , character, real, uint 8) All components have the same type Cell array n n ‘C’‘S’‘ ’‘ 1’‘ 1’‘ 2’ ‘C’‘S’ 1. 1 -7 12 8 1. 1 -1 12 Each cell can store something “bigger” than one value, e. g. , a vector, a matrix, a string The cells may store items of different types Insight Through Computing

Data are often related n n n A point in the plane has an

Data are often related n n n A point in the plane has an x coordinate and a y coordinate. If a program manipulates lots of points, there will be lots of x’s and y’s. Anticipate clutter. Is there a way to “package” the two coordinate values? Insight Through Computing

Packaging affects thinking Our Reasoning Level: P and Q are points. Compute the midpoint

Packaging affects thinking Our Reasoning Level: P and Q are points. Compute the midpoint M of the connecting line segment. Behind the scenes we do this: Mx = (Px + Qx)/2 My = (Py + Qy)/2 Insight Through Computing We’ve seen this before: functions are used to “package’’ calculations. This packaging (a type of abstraction) elevates the level of our reasoning and is critical for problem solving.

Simple example p 1 = struct(‘x’, 3, ’y’, 4); 3 x p 2 =

Simple example p 1 = struct(‘x’, 3, ’y’, 4); 3 x p 2 = struct(‘x’, -1, ’y’, 7); 4 y p 1 D = sqrt((p 1. x-p 2. x)^2 + (p 1. y-p 2. y)^2); D is distance between two points. p 1. x, p 1. y, p 2. x, p 2. y participating as variables—because they are. Insight Through Computing

Creating a structure (by direct assignment) p 1 = struct(‘x’, 3, ’y’, 4); p

Creating a structure (by direct assignment) p 1 = struct(‘x’, 3, ’y’, 4); p 1 is a structure. The structure has two fields. Their names are x and y. They are assigned the values 3 and 4. Insight Through Computing

How to visualize structure p 1 3 4 x y p 1 = struct(‘x’,

How to visualize structure p 1 3 4 x y p 1 = struct(‘x’, 3, ’y’, 4); Insight Through Computing

Accessing the fields in a structure 3 4 x y p 1 A =

Accessing the fields in a structure 3 4 x y p 1 A = p 1. x + p 1. y; Insight Through Computing Assigns the value 7 to A

Assigning to a field in a structure 3 4 x y p 1. x

Assigning to a field in a structure 3 4 x y p 1. x = p 1. y^2; Insight Through Computing Assigns the value 16 to p 1. x

A structure can have fields of different types A = struct(‘name’, ‘New York’, …

A structure can have fields of different types A = struct(‘name’, ‘New York’, … ‘capital’, ‘Albany’, … ‘Pop’, 15. 5) n n Can have combinations of string fields and numeric fields Arguments are given in pairs: a field name, followed by the value Insight Through Computing

Legal/Illegal maneuvers Q = struct(‘x’, 5, ’y’, 6) R = Q % Legal. R

Legal/Illegal maneuvers Q = struct(‘x’, 5, ’y’, 6) R = Q % Legal. R is a copy of Q S = (Q+R)/2 % Illegal. Must access the % fields to do calculations P = struct(‘x’, 3, ’y’) % % Illegal. Args must be in pairs (field name followed by field value) P = struct(‘x’, 3, ’y’, []) % Legal. Use [] as P. y = 4 % place holder Insight Through Computing

Structures in functions function d = dist(P, Q) % P and Q are points

Structures in functions function d = dist(P, Q) % P and Q are points (structure). % d is the distance between them. d = sqrt((P. x-Q. x)^2 +. . . (P. y-Q. y)^2); Insight Through Computing

Example “Make” Function e: l y st ke” d o Go a “ma to

Example “Make” Function e: l y st ke” d o Go a “ma to n use nctio t a fu hligh e’s hig ctur n u o str finiti de function P = Make. Point(x, y) % P is a point with P. x and P. y % assigned the values x and y. P = struct('x', x, 'y', y); Then in a script or some other function… a= 10; b= rand(1); Pt= Make. Point(a, b); % create a point struct % according to definition % in Make. Point function Insight Through Computing

Another function that has structure parameters function Draw. Line(P, Q, c) % P and

Another function that has structure parameters function Draw. Line(P, Q, c) % P and Q are points (structure). % Draws a line segment connecting % P and Q. Color is specified by c. plot([P. x Q. x], [P. y Q. y], c) Insight Through Computing

Insight Through Computing

Insight Through Computing

Pick Up Sticks s = 'rgbmcy'; for k=1: 100 P = Make. Point(randn(1), randn(1));

Pick Up Sticks s = 'rgbmcy'; for k=1: 100 P = Make. Point(randn(1), randn(1)); Q = Make. Point(randn(1), randn(1)); c = s(ceil(6*rand(1))); Draw. Line(P, Q, c) end Generates two random points and chooses one of six colors randomly. Insight Through Computing

Structure Arrays n n n P An array whose components are structures All the

Structure Arrays n n n P An array whose components are structures All the structures must be the same (have the same fields) in the array Example: an array of points (point structures). 5. 86 1. 5. 91. 4. 28 2. 5 1. 8 x y P(1) Insight Through Computing x y P(2) x y P(3) x y P(4)

An Array of Points . 50 . 86 x y P(1) = Make. Point(.

An Array of Points . 50 . 86 x y P(1) = Make. Point(. 50, . 86) Insight Through Computing

An Array of Points -. 50 . 86 x y P(2) = Make. Point(-.

An Array of Points -. 50 . 86 x y P(2) = Make. Point(-. 50, . 86) Insight Through Computing

An Array of Points -1. 0 0. 0 x y P(3) = Make. Point(-1.

An Array of Points -1. 0 0. 0 x y P(3) = Make. Point(-1. 0, 0. 0) Insight Through Computing

An Array of Points -. 5 -. 86 x y P(4) = Make. Point(-.

An Array of Points -. 5 -. 86 x y P(4) = Make. Point(-. 50, -. 86) Insight Through Computing

An Array of Points . 5 -. 86 x y P(5) = Make. Point(.

An Array of Points . 5 -. 86 x y P(5) = Make. Point(. 50, -. 86) Insight Through Computing

An Array of Points 1. 0 0. 0 x y P(6) = Make. Point(1.

An Array of Points 1. 0 0. 0 x y P(6) = Make. Point(1. 0, 0. 0) Insight Through Computing

Function returning an array of points (point structures) function P = Circle. Points(n) theta

Function returning an array of points (point structures) function P = Circle. Points(n) theta = 2*pi/n; for k=1: n c = cos(theta*k); s = sin(theta*k); P(k) = Make. Point(c, s); end Insight Through Computing

Example: all possible triangles n n Place n points uniformly around the unit circle.

Example: all possible triangles n n Place n points uniformly around the unit circle. Draw all possible unique triangles obtained by connecting these points 3 -at-a-time. Insight Through Computing

function Draw. Triangle(P, Q, R, c) % Draw c-colored triangle; % triangle vertices are

function Draw. Triangle(P, Q, R, c) % Draw c-colored triangle; % triangle vertices are points P, % Q, and R. fill([P. x Q. x R. x], . . . [P. y Q. y R. y], c) Insight Through Computing

The following triangles are the same: (1, 3, 6), (1, 6, 3), (3, 1,

The following triangles are the same: (1, 3, 6), (1, 6, 3), (3, 1, 6), (3, 6, 1), (6, 1, 3), (6, 3, 1) Insight Through Computing

Bad! i, j, and k should be different, and there should be no duplicates

Bad! i, j, and k should be different, and there should be no duplicates for i=1: n for j=1: n for k=1: n % Draw a triangle with vertices % P(i), P(j), and P(k) end end Insight Through Computing

All possible (i, j, k) combinations but avoid duplicates. Loop index values have this

All possible (i, j, k) combinations but avoid duplicates. Loop index values have this relationship i < j < k i j k 123 124 125 126 134 135 136 145 146 156 i = 1 Insight Through Computing 234 235 236 245 246 256 i = 2 345 346 356 456 i = 4 i = 3 for i=1: n-2 for j=i+1: n-1 for k=j+1: n disp([i j k]) end end

All possible (i, j, k) combinations but avoid duplicates. Loop index values have this

All possible (i, j, k) combinations but avoid duplicates. Loop index values have this relationship i < j < k for i=1: n-2 for j=i+1: n-1 for k=j+1: n % Draw triangle with % vertices P(i), P(j), P(k) end end Insight Through Computing

All possible (i, j, k) combinations but avoid duplicates. Loop index values have this

All possible (i, j, k) combinations but avoid duplicates. Loop index values have this relationship i < j < k for i=1: n-2 for j=i+1: n-1 for k=j+1: n % Draw triangle with % vertices P(i), P(j), P(k) end end Insight Through Computing

All possible triangles % Drawing on a black background for i=1: n-2 for j=i+1:

All possible triangles % Drawing on a black background for i=1: n-2 for j=i+1: n-1 for k=j+1: n Draw. Triangle( P(i), P(j), P(k), 'm') Draw. Points(P) pause Draw. Triangle(P(i), P(j), P(k), 'k') end end Insight Through Computing

All possible (i, j, k) combinations but avoid duplicates. Loop index values have this

All possible (i, j, k) combinations but avoid duplicates. Loop index values have this relationship i < j < k i j k 123 124 125 126 134 135 136 145 146 156 i = 1 Insight Through Computing 234 235 236 245 246 256 i = 2 345 346 356 456 i = 4 i = 3 for i=1: n-2 for j=i+1: n-1 for k=j+1: n disp([i j k]) end end

Still get the same result if all three loop indices end with n? A:

Still get the same result if all three loop indices end with n? A: Yes B: No i j k 123 124 125 126 134 135 136 145 146 156 i = 1 Insight Through Computing 234 235 236 245 246 256 i = 2 345 346 356 456 i = 4 i = 3 for i=1: n for j=i+1: n for k=j+1: n disp([i j k]) end end

What is the 7 th line of output? for i=1: 5 for j=i+1: 5

What is the 7 th line of output? for i=1: 5 for j=i+1: 5 x = 10*i + j end A: 7 Insight Through Computing B: 21 C: 23 D: 25 E: other

Structures with array fields Let’s develop a structure that can be used to represent

Structures with array fields Let’s develop a structure that can be used to represent a colored disk. It has four fields: xc: x-coordinate of center yc: y-coordinate of center r: radius c: rgb color vector Examples: D 1 = struct(‘xc’, 1, ’yc’, 2, ’r’, 3, … ’c’, [1 0 1]); D 2 = struct(‘xc’, 4, ’yc’, 0, ’r’, 1, … ’c’, [. 2. 5. 3]); Insight Through Computing

Example: Averaging two disks D 2 D 1 Insight Through Computing

Example: Averaging two disks D 2 D 1 Insight Through Computing

Example: Averaging two disks D 2 D 1 Insight Through Computing

Example: Averaging two disks D 2 D 1 Insight Through Computing

Example: Averaging two disks D 2 D 1 Insight Through Computing D

Example: Averaging two disks D 2 D 1 Insight Through Computing D

Example: compute “average” of two disks % D 1 and D 2 are disk

Example: compute “average” of two disks % D 1 and D 2 are disk structures. % Average is: r = (D 1. r + D 2. r) /2; xc = (D 1. xc + D 2. xc)/2; yc = (D 1. yc + D 2. yc)/2; c = (D 1. c + D 2. c) /2; % The average is also a disk D = struct(‘xc’, xc, ’yc’yc, ’r’, r, ’c’, c) Insight Through Computing

How do you assign to g the green-color component of disk D? D= struct(‘xc’,

How do you assign to g the green-color component of disk D? D= struct(‘xc’, 3. 5, ‘yc’, 2, . . . ‘r’, 1. 0, ‘c’, [. 4. 1. 5]) A: g = D. g; B: g = D. c. g; C: g = D. c. 2; D: g = D. c(2); Insight Through Computing E: other