cz1102 computing & problem solving lecture 7

28
Advanced data structure Week 9 By Ji Hui

Upload: charmaine-chu

Post on 09-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CZ1102 Computing & Problem Solving Lecture 7

8/8/2019 CZ1102 Computing & Problem Solving Lecture 7

http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 1/28

Advanced data structure

Week 9By Ji Hui

Page 2: CZ1102 Computing & Problem Solving Lecture 7

8/8/2019 CZ1102 Computing & Problem Solving Lecture 7

http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 2/28

Page 3: CZ1102 Computing & Problem Solving Lecture 7

8/8/2019 CZ1102 Computing & Problem Solving Lecture 7

http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 3/28

Some useful keywords and functions for matrix operations

• end – the index of last row/column

• ‘ (apostrophe) –

Matrix transpose• zeros, ones, eye, rand

– Matrix creation• Find

– Finding all indexes of non ‐zero elements

Page 4: CZ1102 Computing & Problem Solving Lecture 7

8/8/2019 CZ1102 Computing & Problem Solving Lecture 7

http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 4/28

Today’s main topics• Data type for text processing

– char : Characters – string : the array of characters

Advanced data

type:

structure

– representing multiple types of data in a single entity

• Advanced array: cell array – data container that can contain any types of data

Page 5: CZ1102 Computing & Problem Solving Lecture 7

8/8/2019 CZ1102 Computing & Problem Solving Lecture 7

http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 5/28

Page 6: CZ1102 Computing & Problem Solving Lecture 7

8/8/2019 CZ1102 Computing & Problem Solving Lecture 7

http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 6/28

• character – A‐Z – a‐z – 0‐9 – Symbols: #,@,? and etc

• char – enclosing the character in single

quotation marks – e.g. ‘z’

Characters & char

>> letter='#';>> letter

letter =

#

Matlab session

Page 7: CZ1102 Computing & Problem Solving Lecture 7

8/8/2019 CZ1102 Computing & Problem Solving Lecture 7

http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 7/28

Internal representation of char • Internally represented by 16 bit numerical values

– ASCII encoding scheme – e.g. ASCII codes for the letters ‘A’ to ‘Z’ are the

consecutive integers from 65 to 90• Function: char

– convert non ‐negative integer to corresponding char • Functon: double

– Convert char to corresponding integer

Page 8: CZ1102 Computing & Problem Solving Lecture 7

8/8/2019 CZ1102 Computing & Problem Solving Lecture 7

http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 8/28

Page 9: CZ1102 Computing & Problem Solving Lecture 7

8/8/2019 CZ1102 Computing & Problem Solving Lecture 7

http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 9/28

• Definition: a vector of chars• string

– enclosing a sequence of letters in single quotation marks

– e.g. s=‘Hello’

String

>> myname='George'name =George

>> whos mynameName Size Bytes Class Attributes

name 1x6 12 char

Matlab session

Page 10: CZ1102 Computing & Problem Solving Lecture 7

8/8/2019 CZ1102 Computing & Problem Solving Lecture 7

http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 10/28

Special characters• Apostrophe ‘

– Double apostrophe: '' • Blank character

Enclosing a blank

in

single

quatation marks:

‘ ‘

• Creating string

>> s='Hi, this is George'' dog!'

s =Hi, this is George’s dog!

Matlab session

Page 11: CZ1102 Computing & Problem Solving Lecture 7

8/8/2019 CZ1102 Computing & Problem Solving Lecture 7

http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 11/28

String operations• String is a vector, vector operations also work

for string• Concatenation of strings

Joining strings

with

square

brackets

>> course='CZ1102'course =CZ1102>> course=[course,' Problem Solving']course =CZ1102 Problem Solving

Matlab session

Page 12: CZ1102 Computing & Problem Solving Lecture 7

8/8/2019 CZ1102 Computing & Problem Solving Lecture 7

http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 12/28

(cont’)• Comparing strings

– compares the ASCII codes of the two strings element by element

– Two strings must have the same length

>> univ1='nus';univ2='ntu';

univ1 == univ2

ans =

1 0 0

Matlab session

Page 13: CZ1102 Computing & Problem Solving Lecture 7

8/8/2019 CZ1102 Computing & Problem Solving Lecture 7

http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 13/28

Demo of applications• Convert string to lower case

– mylower.m

function u = mylower(s)u = s; % copy string to ufor i=1:length(s) % for each char in s

code = double(s(i)); % finding the ascii codeif (code >=65 && code <=90) % if code corresponds to upper case

code = code + 32; % find the code for lower caseu(i) = char(code); % convert ascii code to lower-char

end;end;

Matlab session

Page 14: CZ1102 Computing & Problem Solving Lecture 7

8/8/2019 CZ1102 Computing & Problem Solving Lecture 7

http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 14/28

Two dimensional strings• Creating a two ‐dimensional character array

– Each row must have the same length – Padding shorter strings with blanks on the right.

>>line0=['Ji Hui '];line1=['Department of Mathematics '];line2=['National university of Singapore'];address=[line0;line1;line2]

address =Ji HuiDepartment of MathematicsNational university of Singapore

Matlab session

Page 15: CZ1102 Computing & Problem Solving Lecture 7

8/8/2019 CZ1102 Computing & Problem Solving Lecture 7

http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 15/28

Easier way to define two ‐dim strings• Function: char

– Automatically padding shorter strings with blanks

• Drawback – wasting storage by saving many blanks.

>> address=char('Ji Hui ',' Department of Mathematics')

address =Ji HuiDepartment of Mathematics

Matlab session

Page 16: CZ1102 Computing & Problem Solving Lecture 7

8/8/2019 CZ1102 Computing & Problem Solving Lecture 7

http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 16/28

Cell• Cell

– Most general data object in Matlab – Can be viewed as a “data container”, that can

contain any types of data: • number, string, matrix, structure and cell itself.

• Definition of cell –

Enclosing the

content

by

curly

backets

– e.g. s={‘department’}

Page 17: CZ1102 Computing & Problem Solving Lecture 7

8/8/2019 CZ1102 Computing & Problem Solving Lecture 7

http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 17/28

(cont’)>> s={'department'}s =

'department'>> whos s

Name Size Bytes Class Attributes

s 1x1 132 cell>>>>s={[1 2;3 4]}s =

[2x2 double]>> whos s

Name Size Bytes Class Attributess 1x1 144 cell

Matlab session

Page 18: CZ1102 Computing & Problem Solving Lecture 7

8/8/2019 CZ1102 Computing & Problem Solving Lecture 7

http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 18/28

From cell to cell array

>>c{1,1} = rand(3);>>c{1,2} = char(’Bongani’, ’Thandeka’);>>c{2,1} = 13;>>c{2,2} = student;>>c

c =[3x3 double] [2x5 char][ 13] 'student'

• Cell array – Using curly brackets to enclose subscripts.

Matlab session

Page 19: CZ1102 Computing & Problem Solving Lecture 7

8/8/2019 CZ1102 Computing & Problem Solving Lecture 7

http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 19/28

More on creating cell array• You can use the curly braces to construct an

entire cell array in one statement:

• Creating empty cell arrays

>>a = cell(1,3) % empty 1-by-3 cell arraya =[] [] []

>>b = {[1:5], rand(2); 'student', char('Jason', 'Amy')}b =

[1x5 double] [2x2 double]'student' [2x5 char ]

Matlab session

Matlab session

Page 20: CZ1102 Computing & Problem Solving Lecture 7

8/8/2019 CZ1102 Computing & Problem Solving Lecture 7

http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 20/28

Accessing data in cell arrays• You can access cell contents using content

indexing (curly braces):

>> c{1,1}

ans =0.7431 0.1712 0.27690.3922 0.7060 0.04620.6555 0.0318 0.0971

>>>> c{2,2}ans =Student

Matlab session

Page 21: CZ1102 Computing & Problem Solving Lecture 7

8/8/2019 CZ1102 Computing & Problem Solving Lecture 7

http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 21/28

Page 22: CZ1102 Computing & Problem Solving Lecture 7

8/8/2019 CZ1102 Computing & Problem Solving Lecture 7

http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 22/28

Structures• Up to now, arrays with only one type of

element: all numeric, or all character• Question: how to represent information with

mixed types

of

elements

• Answer: structure – Structure is a Matlab type that provide the means

that store multiple types of data in a single entity

Page 23: CZ1102 Computing & Problem Solving Lecture 7

8/8/2019 CZ1102 Computing & Problem Solving Lecture 7

http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 23/28

Page 24: CZ1102 Computing & Problem Solving Lecture 7

8/8/2019 CZ1102 Computing & Problem Solving Lecture 7

http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 24/28

Define a structure

>> grade.name='Jason';grade.id='u0920203a';grade.marks=[90, 85, 86,92];

>> grade

grade =

name: 'Jason'id: 'u0920203a'

marks: [90 85 86 92]

Matlab session

Page 25: CZ1102 Computing & Problem Solving Lecture 7

8/8/2019 CZ1102 Computing & Problem Solving Lecture 7

http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 25/28

How to access fields.• Using dot to separate structure name and

field name

>> cname=grade.name

cname =

Jason>> mterm = grade.marks(3)

mterm =

86

Matlab session

Page 26: CZ1102 Computing & Problem Solving Lecture 7

8/8/2019 CZ1102 Computing & Problem Solving Lecture 7

http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 26/28

Structure array• To use array of structure, use subscripts after

the structure name

>>grade(2).name='Sara';

grade(2).id = 'u1020202020';grade(2).marks = [76 64 83 72];>> grade

grade =

1x2 struct array with fields:nameid

marks

Matlab session

Page 27: CZ1102 Computing & Problem Solving Lecture 7

8/8/2019 CZ1102 Computing & Problem Solving Lecture 7

http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 27/28

Page 28: CZ1102 Computing & Problem Solving Lecture 7

8/8/2019 CZ1102 Computing & Problem Solving Lecture 7

http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 28/28