CZ1102 Computing & Problem Solving Lecture 7
-
Upload
charmaine-chu -
Category
Documents
-
view
218 -
download
0
Transcript of 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
8/8/2019 CZ1102 Computing & Problem Solving Lecture 7
http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 2/28
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
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
8/8/2019 CZ1102 Computing & Problem Solving Lecture 7
http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 5/28
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
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
8/8/2019 CZ1102 Computing & Problem Solving Lecture 7
http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 8/28
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
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
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
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
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
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
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
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’}
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
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
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
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
8/8/2019 CZ1102 Computing & Problem Solving Lecture 7
http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 21/28
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
8/8/2019 CZ1102 Computing & Problem Solving Lecture 7
http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 23/28
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
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
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
8/8/2019 CZ1102 Computing & Problem Solving Lecture 7
http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 27/28
8/8/2019 CZ1102 Computing & Problem Solving Lecture 7
http://slidepdf.com/reader/full/cz1102-computing-problem-solving-lecture-7 28/28