Structures, Structure Array, Structure with Array Fields | CS 1112, Study notes of Computer Science

Material Type: Notes; Class: Introduction to Computing Using MATLAB; Subject: Computer Science; University: Cornell University; Term: Fall 2008;

Typology: Study notes

Pre 2010

Uploaded on 08/31/2009

koofers-user-wul
koofers-user-wul 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS1112 Lecture 19 Nov 4, 2008
Lecture slides 1
Previous Lecture:
Cell arrays
Today’s Lecture:
Structures
Structure array (an array of structures)
A structure with array fields
Announcement:
Section this week in the lab (UP B7)
CMCM info sessions: 11/4 6pm 215 Malott; 11/12 6pm
253 Malott
November 4, 2008 Lecture 19 7
A point in the plane has an x coordinate and 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?
Data are often related
November 4, 2008 Lecture 19 8
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
Packaging affects thinking
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.
November 4, 2008 Lecture 19 9
Simple example
p1 = struct(‘x’,3,’y’,4);
p2 = struct(‘x’,-1,’y’,7);
D = sqrt((p1.x-p2.x)^2 + (p1.y-p2.y)^2);
Dis distance between two points.
p1.x, p1.y, p2.x, p2.y participating
as variables—because they are.
November 4, 2008 Lecture 19 10
Creating a structure (by direct assignment)
p1 = struct(‘x’,3,’y’,4);
p1 is a structure.
The structure has two fields.
Their names are xand y.
They are assigned the values 3 and 4.
November 4, 2008 Lecture 19 11
p1
x y
34
p1 = struct(‘x’,3,’y’,4);
How to visualize structure p1
pf3
pf4
pf5

Partial preview of the text

Download Structures, Structure Array, Structure with Array Fields | CS 1112 and more Study notes Computer Science in PDF only on Docsity!

„ Previous Lecture: „ Cell arrays

„ Today’s Lecture: „ Structures „ Structure array (an array of structures) „ A structure with array fields

„ Announcement: „ Section this week in the lab (UP B7) „ CMCM info sessions: 11/4 6pm 215 Malott; 11/12 6pm 253 Malott November 4, 2008 Lecture 19 7

„ A point in the plane has an x coordinate and 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?

Data are often related

November 4, 2008 Lecture 19 8

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)/ My = (Py + Q (^) y)/

Packaging affects thinking

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.

November 4, 2008 Lecture 19 9

Simple example p1 = struct(‘x’,3,’y’,4);

p2 = struct(‘x’,-1,’y’,7);

D = sqrt((p1.x-p2.x)^2 + (p1.y-p2.y)^2);

D is distance between two points.

p1.x, p1.y, p2.x, p2.y participating as variables—because they are.

November 4, 2008 Lecture 19 10

Creating a structure (by direct assignment)

p1 = struct(‘x’,3,’y’,4);

p1 is a structure. The structure has two fields. Their names are x and y. They are assigned the values 3 and 4.

November 4, 2008 Lecture 19 11

p

x y

p1 = struct(‘x’,3,’y’,4);

How to visualize structure p

November 4, 2008 Lecture 19 12

p

x y

A = p1.x + p1.y; Assigns the value 7 to A

Accessing the fields in a structure

November 4, 2008 Lecture 19 13

p

x y

p1.x = p1.y^2; (^) Assigns the value 16 to p1.x

Assigning to a field in a structure

November 4, 2008 Lecture 19 14

A = struct(‘name’, ‘New York’,… ‘capital’, ‘Albany’,… ‘Pop’,15.5)

Can have combinations of string fields and numeric fields.

A structure can have fields of different types

November 4, 2008 Lecture 19 15

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

November 4, 2008 Lecture 19 16

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

Structures in functions

November 4, 2008 Lecture 19 17

Sample “Make” Function

function P = MakePoint(x,y) % P is a point with P.x and P.y % assigned the values x and y.

P = struct('x',x,'y',y);

Good Style—highlights the structure’s definition

November 4, 2008 Lecture 19 24

P(3)

x y

An Array of Points

P(3) = MakePoint(-1.0,0.0) November 4, 2008 Lecture 19 28

function P = CirclePoints(n)

theta = 2pi/n; for k=1:n c = cos(thetak); s = sin(theta*k); P(k) = MakePoint(c,s); end

Function returning an array of points (point structures)

November 4, 2008 Lecture 19 29

„ Place n points uniformly around the unit circle. „ Draw all possible triangles obtained by connecting these points 3-at-a-time.

Example: all possible triangles

November 4, 2008 Lecture 19 31

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

fill([P.x Q.x R.x P.x], ... [P.y Q.y R.y P.y], c)

November 4, 2008 Lecture 19 32

( i , j , k ) = ( 1 , 3 , 6 ) 2 1

3

4 5

6

( i , j , k ) = ( 1 , 4 , 5 ) 2 1

3

4 5

6

These triangles are the same: (1,4,6), (1,6,4), (4,1,6), (4,6,1), (6,1,4), (6,4,1)

November 4, 2008 Lecture 19 33

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 end

Bad! i, j, and k should be different

November 4, 2008 Lecture 19 34

for i=1:n for j=i+1:n for k=j+1:n disp([i j k]) end end end

All possible (i,j,k) but avoid duplicates: i < j < k

November 4, 2008 Lecture 19 35

1 2 3 1 2 4 1 2 5 1 2 6 1 3 4 1 3 5 1 3 6 1 4 5 1 4 6 1 5 6

2 3 4 2 3 5 2 3 6 2 4 5 2 4 6 2 5 6

3 4 5 3 4 6 3 5 6

4 5 6

i = 1

i = 4 i = 3

i = 2

November 4, 2008 Lecture 19 36

What is the 7th^ line of output?

for i=1: for j=i+1: x = 10*i + j end end

A: 7 B: 21 C: 23 D: 25 E:other

November 4, 2008 Lecture 19 37

% Drawing on a black background for i=1:n for j=i+1:n for k=j+1:n DrawTriangle( P(i),P(j),P(k),'m') DrawPoints(P) pause DrawTriangle(P(i),P(j),P(k),'k') end end end

All possible triangles

November 4, 2008 Lecture 19 38

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: D1 = struct(‘xc’,1,’yc’,2,’r’,3,… ’c’,[1 0 1]); D2 = struct(‘xc’,4,’yc’,0,’r’,1,… ’c’,[.2 .5 .3]); November 4, 2008 Lecture 19 39

Example: compute “average” of two disks % D1 and D2 are disk structures. % Average is: r = (D1.r + D2.r) /2; xc = (D1.xc + D2.xc)/2; yc = (D1.yc + D2.yc)/2; c = (D1.c + D2.c) /2;

% The average is also a disk D = struct(‘xc’,xc,’yc’yc,’r’,r,’c’,c)