Sie sind auf Seite 1von 28

Advanced data structure

Week 9

By Ji Hui
Reviews of last week
• Matrix in Linear Algebra • Matrix object in Matlab

 a11 a12 a13  a1n  Math notation Matlab notation


a a a  a 
 21 22 23 2n 
,
, )  a 31 a 32 a 33  a 3n 
 
Row index        ( , size
a a a  a 
 m1 m 2 m 3 mn 
Column index i‐th row A(i,:)

j‐th column A(:,j)


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
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
Text processing
• Data structure for text processing
Text Data structure

Article Cell
or books array
Word & 
Sentence String

Characters Char
Characters & char
• character  Matlab session
– A‐Z  >> letter='#';
– a‐z  >> letter

– 0‐9  letter =
– Symbols: #,@,? and etc
#
• char
– enclosing the character in single 
quotation marks
– e.g. ‘z’
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
ASCII code, char and double
Matlab session
>> c=char(70) % character of ASCII code 70
c=
F
>> char(c+1) % character after ‘F’ in ASCII table
ans =
G
>> double(c) % ASCII code for ‘F’
ans =
70
>> char(65:128) % list part of ASCII table
ans =
ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvw
xyz{|}~
String
• Definition: a vector of  chars
• string
– enclosing a sequence of letters  in single quotation 
marks
Matlab session
– e.g. s=‘Hello’ >> myname='George'
name =
George

>> whos myname


Name Size Bytes Class Attributes

name 1x6 12 char


Special characters
• Apostrophe ‘
– Double apostrophe: '' 
• Blank character
– Enclosing a blank in single quatation marks: ‘ ‘ 
• Creating string
Matlab session
>> s='Hi, this is George'' dog!'

s=
Hi, this is George’s dog!
String operations
• String is a vector, vector operations also work 
for string
• Concatenation of strings
– Joining strings with square brackets
Matlab session
>> course='CZ1102'
course =
CZ1102
>> course=[course,' Problem Solving']
course =
CZ1102 Problem Solving
(cont’)
• Comparing strings
– compares the ASCII codes of the two strings 
element by element
– Two strings must have the same length
Matlab session
>> univ1='nus';
univ2='ntu';
univ1 == univ2

ans =

1 0 0
Demo of applications
• Convert string to lower case
– mylower.m
Matlab session
function u = mylower(s)
u = s; % copy string to u
for i=1:length(s) % for each char in s
code = double(s(i)); % finding the ascii code
if (code >=65 && code <=90) % if code corresponds to upper case
code = code + 32; % find the code for lower case
u(i) = char(code); % convert ascii code to lower-char
end;
end;
Two dimensional strings
• Creating a two‐dimensional character array 
– Each row must have the same length
– Padding shorter strings with blanks on the right.
Matlab session
>>line0=['Ji Hui '];
line1=['Department of Mathematics '];
line2=['National university of Singapore'];
address=[line0;line1;line2]

address =
Ji Hui
Department of Mathematics
National university of Singapore
Easier way to define two‐dim strings
• Function: char
– Automatically padding shorter strings with blanks
Matlab session
>> address=char('Ji Hui ',' Department of Mathematics')

address =
Ji Hui
Department of Mathematics

• Drawback
– wasting storage by saving many blanks.
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’}
(cont’)
Matlab session
>> 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 Attributes


s 1x1 144 cell
From cell to cell array

• Cell array
– Using curly brackets to enclose subscripts.
Matlab session
>>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'
More on creating cell array
• You can use the curly braces to construct an 
entire cell array in one statement:
Matlab session
>>b = {[1:5], rand(2); 'student', char('Jason', 'Amy')}
b=
[1x5 double] [2x2 double]
'student' [2x5 char ]

• Creating empty cell arrays
Matlab session
>>a = cell(1,3) % empty 1-by-3 cell array
a=
[] [] []
Accessing data in cell arrays
• You can access cell contents using content 
indexing (curly braces):
Matlab session
>> c{1,1}
ans =
0.7431 0.1712 0.2769
0.3922 0.7060 0.0462
0.6555 0.0318 0.0971
>>
>> c{2,2}
ans =
Student
Summary of differences between 
matrices and cell arrays
Operations Matrix Cell array

Creating [a, b, c] {a, b, c}


element
Accessing  A(i) A{i}
element
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
Introduction to structure
• field
– data container store one type of data
• Illustration of a structure grade with three 
fields name, id, marks

name 1x64 char
grade

id 1x16 char

mark 1x4 double
Define a structure
Matlab session
>> grade.name='Jason';
grade.id='u0920203a';
grade.marks=[90, 85, 86,92];
>> grade

grade =

name: 'Jason'
id: 'u0920203a'
marks: [90 85 86 92]
How to access fields.
• Using dot to separate structure name and 
field name
Matlab session
>> cname=grade.name

cname =

Jason
>> mterm = grade.marks(3)

mterm =

86
Structure array
• To use array of structure, use subscripts after 
the structure name
Matlab session
>>grade(2).name='Sara';
grade(2).id = 'u1020202020';
grade(2).marks = [76 64 83 72];
>> grade

grade =

1x2 struct array with fields:


name
id
marks
Accessing fields of elements in array
Matlab session
>> grade(2).name

ans =

Sara

>> grade(2).marks(1:3)

ans =

76 64 83
Summary
• Char
• String
• 2D string array
• Cell array
• Structure
• Structure array

Das könnte Ihnen auch gefallen