Download Lecture 8

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Mathematics of radio engineering wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Catuṣkoṭi wikipedia , lookup

Transcript
Lecture 15
Chapters 6 and 7
Outline from Chapter 6
6.1 Character String Concepts: Mapping and Casting
6.2 MATLAB Implementation
6.2.1 Slicing and Concatenating Strings
6.2.2 Arithmetic and Logic Operations
6.2.3 Useful Functions
6.3 Format Conversion Functions
6.3.1 Conversion from Numbers to Strings
6.3.2 Conversion from Strings to Numbers
6.4 Character String Operations
6.4.1 Simple Data Output: the disp(…) function
6.4.2 Complex Output
6.4.3 Comparing Strings
6.5 Arrays of Strings
Characters and Numbers
• Unicode: provides a unique number for every
character,
no matter what the platform,
no matter what the program,
no matter what the language.
http://unicode.org/standard/WhatIsUnicode.html
• ASCII: a smaller code for fewer characters,
represents characters with binary number code
• It is certainly possible to have characters that are
digits. ‘124’
Character Mapping
• A kind of mapping, i.e., “one from column A is
related to one from column B”
• Specifically, an element from column A is a
character, such as ‘r’ or ‘\tab’ or ‘\bell’ and
an element from column B is a number.
• When the context implies that a number is to
be interpreted as a character, the mapping is
used in reverse to convert the number into its
corresponding character.
Casting
• The default context comes from the type of a variable, which comes
from the last time it was assigned.
• We can force the context by casting.
>> myArray = [97 98 99 100 101]
myArray =
97 98 99 100 101
>> myString = char(myArray)
myString =
abcde
>> uint8(myString)
ans =
97 98 99 100 101
Implicit Casting
>> fred='Fred'
fred =
Fred
>> next = fred+1 <- contents of variable fred interpreted
in the context of number, due to the +1
next =
71 115 102 101
Extraction from, Combination of
Strings
>> firstName = 'George Washington';
>> lastName = 'Carver';
>> wholeName = [firstName, ' ',lastName]
wholeName =
George Washington Carver
>> middleName = wholeName(8:17)
middleName =
Washington
Logic on Character Strings
middleName>'Z'
ans =
Columns 1 through 6
0 1 1 1 1 1
Columns 7 through 10
1 1 1 1
>> ischar(wholeName)
ans =
1
>> isspace(wholeName)
ans =
Columns 1 through 6
0 0 0 0 0 0
Columns 7 through 12
1 0 0 0 0 0
Columns 13 through 18
0 0 0 0 0 1
Columns 19 through 24
0 0 0 0 0 0
Character Strings of Digits
• People using MATLAB intend numerical
interpretation, but type character strings of
digits.
• There is a more efficient numerical
representation used within MATLAB, and the
computer in general, than character strings of
digits.
• MATLAB performs the conversions between
the two.
To String - 1
>> y = sqrt(391)
>> num2str(y,9)
y=
ans =
19.7737
19.7737199
>> int2str(y)
>> num2str(y, 15)
ans =
ans =
20
19.7737199332852
To String - 2
>> niceString = sprintf('Here are some tabbed
columns:\n%f\t%f\t%f\t%f',...
123,456,789, 99)
niceString =
Here are some tabbed columns:
123.000000
456.000000
789.000000
99.000000
>> niceStringdc = sprintf('Here are some tabbed
columns:\n%.2f\t%.2f\t%.2f\t%.2f',...
123,456,789, 99)
niceStringdc =
Here are some tabbed columns:
123.00 456.00 789.00 99.00
From String to Number—If…
•
>> goodString = '124'
•
goodString =
>> sscanf(nonNumericString,'%d')/2
•
124
ans =
•
>> nonNumericString = '4r5t6y'
•
nonNumericString =
•
4r5t6y
•
>> sscanf(goodString,'%d')/2
•
ans =
•
62
•
>>
2
Data Output
>>disp(‘the answer is’)
the answer is
>> fprintf('the answer is %s makes perfect,
maybe %d%% of the time', 'practice', 75)
the answer is practice makes perfect, maybe
75% of the time>>
Why is the prompt at the end?
String Output
>> fprintf('the answer is %s makes perfect,
maybe %d%% of the time\n', 'practice', 75)
the answer is practice makes perfect, maybe
75% of the time
>>
What’s the difference? The \n is a newline.
String Comparison
>> firstString = 'ATGC'
firstString =
ATGC
>> secondString = 'ATTC'
secondString =
ATTC
>> firstString == secondString
ans =
1 1 0 1
>> thirdString = 'ATT'
thirdString =
ATT
>> firstString == thirdString
??? Error using ==> eq
Matrix dimensions must agree.
>> strcmp(firstString, thirdString)
ans =
0
More String Comparison
>> thirdString = 'ATT'
thirdString =
ATT
>> fourthString = 'att'
fourthString =
att
>> strcmp(thirdString, fourthString)
ans =
0
>> strcmpi(thirdString, fourthString)
ans =
1
Arrays of Strings
• Remember that arrays have a number of columns
• That number of columns applies to every row.
• To make strings of all the same length, one way is:
>> aStringArray=char('Timoshenko', 'Maxwell', 'Mach')
aStringArray =
Timoshenko
Maxwell
Mach
Outline from Chapter 7 -- 1
• 7.1 concept: Collecting Dissimilar Objects
• 7.2 Cell Arrays
– 7.2.1 Creating Cell Arrays
– 7.2.2 Accessing Cell Arrays
– 7.2.3 Using Cell Arrays
• 7.3 MATLAB Structures
– 7.3.1 Constructing and Accessing One Structure
– 7.3.2 Constructor Functions
Outline from Chapter 7 -- 2
• 7.4 Structure Arrays
– 7.4.1 Constructing Cell Arrays
– 7.4.2 Accessing Structure Elements
– 7.4.3 Manipulating Structures
Three Heterogeneous Collections
• Cell arrays : index their contents with a
numerical index
• Structures : identify components with
symbolic index
• Structure arrays : index member structures
with numerical index
Accessing Data Within Heterogeneous
Collections
• Access by index (much as we have been doing
with arrays), called cell arrays
• Access by name (name of field within
structure)
• Can create arrays of structures
Creating Cell Arrays
• Cell arrays can be created by assigning values to an
indexed variable.
>> A{1}=42
A=
[42]
>> B{1}={[4 6]}
B=
{1x1 cell}
>> C={3,[1,2,3], 'abcde'}
C=
[3] [1x3 double] 'abcde'
Accessing Cell Arrays
• (conventional) Arrays of Containers
• Compare accessing the array to obtain the
container
vs
accessing the content of the container
Example Create Cell Array D
>> D = [A B C {'xyz'}]
D=
Columns 1 through 4
[42] {1x1 cell} [3] [1x3 double]
Columns 5 through 6
'abcde' 'xyz'
Example Access Cell Array
• Access a container in D, familiar notation
>> D(1)
ans =
[42]
• Access contents of container in D
>> D{1}
ans =
42
More Accessing Cell Array
>> a={3,[1,2,3] 'abcde'}
a=
[3] [1x3 double] 'abcde'
>> a{1:2}
ans =
3
ans =
1 2 3
>> [x y]=a{1:2}
x=
3
y=
1 2 3
>> b([1 3])=a([1 2])
b=
[3] [] [1x3 double]
deal Function -- 1
• Access the cell array:
• >> help deal
• DEAL Deal inputs to outputs.
•
[A,B,C,...] = DEAL(X,Y,Z,...) simply matches up the
input and
•
output lists. It is the same as A=X, B=Y, C=Z, ...
•
[A,B,C,...] = DEAL(X) copies the single input to all
•
the requested outputs. It is the same as A=X, B=X,
C=X, ...
•
Operations on
Heterogeneous Collections
• With multiple datatypes
making up the
heterogeneous collections,
collective operations do not
really apply
• Instead, first extract the
part, then do the
appropriate operation, then
reassemble
aStringArray =
Timoshenko
Maxwell
Mach
>> aStringArray+1
ans =
Columns 1 through 8
85 106 110 112 116 105 102 111
78 98 121 120 102 109 109 33
78 98 100 105 33 33 33 33
Columns 9 through 10
108 112
33 33
33 33
>> char(aStringArray+1)
ans =
Ujnptifolp
Nbyxfmm!!!
Nbdi!!!!!!
Processing with Cell Arrays
<initialize result>
for <index specification>
<extract an element>
<check the element accordingly>
<process the element accordingly>
end
<finalize result>
Extract, Check “Accordingly”
• Find the class of an element: class(item) returns a
string suitable for use in a switch statement
• isa(item, ‘class’)
• iscell(item)
• ischar(item)
• islogical(item)
• isnumeric(item)
• isstruct(item)
A Little More Cell Processing
<initialize result>
for <index specification>
<extract an element>
<check the element
accordingly>
<process the element
accordingly>
end
<finalize result>
function ans = totalNums(ca)
%count the numbers in a cell
array
ans = 0;
for i = 1:length(ca)
item = ca{i}
if isnumeric(item)
ans =
ans+prod(size(item));
end
end
Structures
• Heterogeneous, like cell arrays
• Instead of accessing by sequence number,
access by field name
• Have fields,
– Name
– Datatype
• Any MATLAB object
• Can have arrays of structures
Creating One Structure*
• The structure is created
incrementally—it emerges:
>> myStruct.item1 = 3
myStruct =
item1: 3
myStruct.goodFieldName = 'example'
myStruct =
item1: 3
item2: 'abce'
goodFieldName: 'example‘
>> myStruct.item2 = 'abce'
>> myOtherStruct = rmfield(myStruct, 'item2')
myStruct =
myOtherStruct =
item1: 3
item2: 'abce'
item1: 3
goodFieldName: 'example'
*Created as a variable, not as a datatype
Another Way to Create One Structure*
>> struct('studentName', 'Elaine',...
'targetDegree', 'PhD',...
'nChildren', 1,...
'location', 'home',...
'age', 65)
ans =
studentName: 'Elaine'
targetDegree: 'PhD'
nChildren: 1
location: 'home'
age: 65
*Created as a variable, not as a datatype
A Way to Create Multiple Instances of
One Structure*
function ans = makeCD(gn, ar, ti, yr, st, pr)
%populate CD structure with provided CD data
ans.genre = gn;
ans.artist = ar;
ans.title = ti;
ans.year = yr;
ans.stars = st;
ans.price = pr;
end
*Created as a variable, not as a datatype, but the function fills the template as if it were a type.
Function in Action
CD=makeCD('Blues', 'Charles, Ray', 'Genius Loves
Company', 2004, 4.5, 15.35)
CD =
genre: 'Blues'
artist: 'Charles, Ray'
title: 'Genius Loves Company'
year: 2004
stars: 4.5000
price: 15.3500
Many Instances of One Type of
Structure
• An array of data items, each of which contains
the same fields of information.
>> cds(1) = makeCD('Blues', 'Clapton, Eric', ... >> cds(2)=makeCD('Classical',...
'Sessions For Robert J', 2004, 2, 18.95)
'Bocelli, Andrea', 'Andrea', 2004, 4.6, 14.89)
cds =
genre: 'Blues'
artist: 'Clapton, Eric'
title: 'Sessions For Robert J'
year: 2004
stars: 2
price: 18.9500
>>
cds =
1x2 struct array with fields:
genre
artist
title
year
stars
price