cz1102 computing & problem solving lecture 12
TRANSCRIPT
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
1/34
Review
Week 14
By JI, Hui
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
2/34
Exam
Closed book, two A4 sizes double-sides help
sheet are allowed
Exam time 2 hours
No calculator or computer is allowed.
Totally 6 questions
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
3/34
Questions
Determine which the commands is valid or not e.g. A?=7+1.5
Find the value of the expression e.g. [1 3] .* [1 ,4]
What is displayed when the block of code is executed e.g. a=0;
for k = 1:4
a = a + 2*k;
end;
disp(a);
Write a function such that it takes ***** as input andoutput its ********
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
4/34
Variable
Rule for name of variable
consist only of the letters az, AZ, the digits 09
and the underscore _
Starting with a letter
Reserved wordcan not be used as the variable
name
Matlab is case sensitive Matlab distinguishes between upper- and lower-case
letters.
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
5/34
Assignment
Format of variable assignment
variablename = expression
Expression is executed and store the result in
variablename
= does not mean equal, it means store
A semicolon in the end of expression will
suppress the output
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
6/34
Expression
Allowable items in expression
Values
Variables (already defined)
Operators
Pre-defined functions
Matlab
User
Parenthesis
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
7/34
Precedency of operators
Precedency of operators (from the first to the last)1. ()
2. ^
3. - (Negation)
4. *,\,/5. +,-
Within the same precedency level Expression is evaluated from right to left
Question: what is the value of a*b/c?
Nested parenthesis Inner parenthesis is evaluated first
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
8/34
Data type of numbers
Integer
int8, int16, int32, int64: integers in 8,16,32,64
bits)
uint8, uint16, uint32,uint64: unsigned integers
Non-integer
single: single-precision numbers
Double: double-precision numbers
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
9/34
Logical expression
Evaluate the correctness of the statement
TRUE
FALSE
Six relationship operator1. == equal
2. ~= not equal
3. > greater than
4. >= greater than or equal to
5. < less than
6.
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
10/34
Logical operator and values
logical operators
~: not
&: and
|: or
Logical value
1 (true)
0 (true)
10
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
11/34
Logical operator precedence
Precedence (from the first to the last)
Parenthesis ()
Arithmetic operators
Relationship operators
==,~=,>,>=,
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
12/34
The one-line if statement
ifcondition
statement
end
condition is usually a logical expression
Ifcondition is true, statementis executed, but if
condition is false, nothing happens.
12
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
13/34
More on conditional statemet
ifcondition
statementA
else
statementB
end
condition is usually a logical expression
Ifcondition is true, statementA is executed, but ifcondition is false, statementB is executed.
Dont forget end, or MATLAB will wait forever.
13
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
14/34
elseifladder
ifcondition1
statementsA
elseifcondition2
statementsBelseifcondition3
statementsC
else
statementsE
end
14
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
15/34
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
16/34
Vector
Vector
Also called list, arrays in other language
Row vector
[1,2,3,4]
A vector can also be generated (initialized)
with the colon operator
J:m:k
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
17/34
How to access items in vector
Subscript
x(k): k-th element of vector x
A subscript is indicated inside roundbrackets
(also called parentheses).
A subscript may be a scalar or a vector.
x(j:k): the vector of j-th, (j+1)-th, , k-th elements of
vector x
In MATLAB subscripts always start at 1.
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
18/34
Creating matrix
Creating matrix use a semicolon to indicate the end of a row
e.g. >> a = [1,2,3; 4, 5,6]
a =
1 2 34 5 6
Individual elements of a matrix are referenced withtwo subscripts, the first for the row, and the second for
the column e.g. >> a(1,2)
Alternatively, you may use a single index by think amatrix as being unwounded column by column
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
19/34
Some useful functions for matrix
operations
size
transpose using
reshape diag
find
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
20/34
Matrix arithmetic operations
Some operations are not element-wise
Multiplication
Exponential
Some operations are element-wise
Addtion
Subtraction
Using . to specify the element-wise operations
Keeping the dimension of matrices consistent
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
21/34
The keyword end in subscripts.
The keyword end refers to the lastrow or
column of an array.
There is a group of functions to generate
elementary matrices
eye, zeros, ones
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
22/34
for struct
The for struct
forindex=j:k
statements
End
or
forindex=j:m:k
statements
end
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
23/34
While struct
while struct
while condition
statements
end The while construct repeats statements WHILE its
condition remains true
The condition is tested each time BEFORE
statements are repeated. condition must depend on statements in some
way, otherwise the loop will never end.
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
24/34
Finishing loops earlier before its end
using break
break struct
while condition for i = j:k,
statementsA statementsA
if (condionB), break; end; if (conditionB), break;end;statementsB statementsB
end end
During each repetition, after statemensA isexecuted, if conditionB holds true, the whileloops will end immediately and go to the next lineafter the loop.
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
25/34
Execute part of statements during the
loops using continue
continue structwhile condition for i = j:k,
statementsA statementsA
if (condionB), continue;end; if (conditionB) continue;endstatementsB statementsB
end end
At each repetition, after statementsA is executed, if
conditionB holds true, statementsB will not beexecuted and the program will start the nextrepetition.
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
26/34
function
A function M-file name.m has the following
general form:
function [ outarg1, outarg2, ] = name(
inarg1, inarg2, )
...
outarg1 = ... ;
Outarg2= ;
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
27/34
More on rules of function
Keyword function
Input and output arguments.
Function names must follow the MATLAB rulesfor variable names.
Any variables defined inside a function are
local, inaccessible outside the function.
Recursive function
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
28/34
data file by load and save
save myData A
save variable A into file mydata.mat
A = load(myData.txt)Import text (ASCII) data to A
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
29/34
character
A-Z
a-z
0-9 Symbols: #,@,& and etc
Notation ofchar
enclosing the character in singlequotation marks
e.g. z
char
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
30/34
Definition: a vector of chars
string
enclosing a sequence of letters in single quotation
marks
Apostrophe '
Double apostrophe: ''
Blank character
Enclosing a blank in single quotation marks: ' '
String
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
31/34
Cell & Structure
Cell
Most general data object in Matlab
Enclosing the content by curly brackets
Structure
field
data container store one type of data
Using dot to separate structure name and field
name
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
32/34
Graphics
plot
plot3
mesh
surf
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
33/34
Efficient programming
Space pre-allocation
Vecterization to avoid loops find
Min
max sum,
cumsum,
prod,
cumprod
sort Diff
Deleting Sub-matrices with [ ]
-
8/8/2019 CZ1102 Computing & Problem Solving Lecture 12
34/34
Image processing
imread
imwrite
Binary image
Greyscale image
Color image
Indexed color image