Week 1-2 (2)
Week 1-2 (2)
Rounding functions
fix() - Round to the nearest integer towards zero.
floor() - Round to the nearest integer towards minus infinity.
ceil() - Round to the nearest integer towards plus infinity.
round() - Round towards nearest integer.
ans = ans =
3 - 4
• Example 3: >> ceil(3.1415) • Example 4: >> round(-3.1415)
ans = ans =
4 - 3
2
Rounding functions
fix() - Round to the nearest integer towards zero.
floor() - Round to the nearest integer towards minus infinity.
ceil() - Round to the nearest integer towards plus infinity.
round() - Round towards nearest integer.
ans = ans =
- 2 0
• Example 7: >> round(0.5) In the case of a tie, where an
element has a fractional part of
ans = exactly 0.5, the round function
rounds away from zero to the
1 integer with larger magnitude.
3
Random numbers
7.3070
rand * N
4
Random numbers
5
Random numbers
>> randi(100)
ans =
93
6
Random numbers
>> randi([20,35])
ans =
27
7
Some Functions in elfun
>> help elfun
Function Description
sin, cos, tan sine, cosine, tangent in radians
ans = 4
• Note 1: The order of arguments in rem and mod does matter.
• Note 2: Don’t confuse the value e (Euler’s number) with the e used
in MATLAB to specify an exponent for scientific notation.
>> 10e3
ans = 9
10000
Relational Expressions
• Expressions that are conceptually either true or false are called
relational expressions, or Boolean or logical expressions
• “true” is represented by the logical value 1, and “false” is
represented by the logical value 0
Operator name
> greater than
>= greater than or equal to
< less than
<= less than or equal to
== equal to
~= not equal to
ans =
1
• Example: >> 2 ~= 4
ans =
11
Relational Expressions: relational
operators
ans =
1
>> ans + 3
ans =
4
12
Relational Expressions: relational
operators
ans =
0
ans =
13
Review: Operator Precedence Rules
^ Exponentiation
- Negation
*, /, \ Multiplication and division
+, - Addition and subtraction • Lowest
14
Operators Precedence Rules (Update 1):
^ Exponentiation or power
- Negation
*, /, \ Multiplication and division
+, - Addition and subtraction
<, <=, >, relational • Lowest
>=, ==, ~=
15
Relational Expressions: relational
operators
• Example: >> 9 > 8 > (7 < 6)
9 > 8 > 0
1 > 0
Note: Be careful about using
equality and inequality
ans = operators.
1
Occasionally roundoff errors
appear!
• Example: >> cos (pi/2) == 0
>> cos (pi/2)
ans =
0 ans =
6.1232e-17
16
Relational Expressions: logical operators
x
y
• The logical operators in MATLAB are:
||
Operator name
|| or Switch Representation
&& and of OR Function
x y
~ not
&&
• The “or” logical operator will output a true
value if either or both of the operands are true.
Switch Representation
• The “and” logical operator will output a true of AND Function
value only if both of the operands are true.
17
Relational Expressions: logical operators
x
y
• Summary: Truth Table for logical operators:
||
OR Function
x y ~x x || y x && y
True True False True True x y
True False False True False
False True True True False &&
False False True False False
AND Function
18
Relational Expressions: logical operators
• The || and && operators in MATLAB are also known as short-circuit
operators.
• This means that if the result of the expression can be determined from
the first part, then the second part will not even be evaluated .
• Example:
According to the truth table, if any
>> b = 0; operands of an AND expression are
false, the entire expression must be
>> (b ~= 0) && (a/d > 18)
false. So, if (b ~= 0) evaluates to false,
MATLAB assumes the entire
0 expression to be false and terminates
ans = its evaluation of the expression early.
0
19
Relational Expressions
1 || ...
20
Operators Precedence Rules:
^ Exponentiation or power
-, ~ Negation, not
*, /, \ Multiplication and division
+, - Addition and subtraction
<, <=, >, relational
>=, ==, ~=
&& and
|| or • Lowest
21
Relational Expressions
• Example: operator
>> 3 == 3 + 3
( )
ans =
^
0
-, ~
1 || 0
ans =
22
1
Relational Expressions
• Example:
What would be the value of this expression
>> -5 < x < 3
If the value of x is 0?
What if the value of x is -10?
1 < 3 or 0 < 3
• Question:
5 x 3
24
Relational Expressions
ans =
http://www.asciitable.com/
25
Common Errors
• Putting space in a variable name
• The dimensions of a matrix are r × c, where r is the number of rows and c is the
number of columns.
3x2
1x1 1x4
28
Scalars
• A scalar in MATLAB is an array of size 1 x 1
1
• Example:
>> z = 6 a variable that stores a single value is an
example of a scalar
z =
29
Vectors
• A vector in MATLAB is equivalent to a 1-dimensional array where one of the
size of the dimensions is 1
• There are: row vectors (1×n) and column vectors (n×1)
30
Row Vectors
• A row vector in MATLAB is a 1 x n array
• Example:
>> v = [1 2 3 4]
v =
1 2 3 4
>> v = [1, 2, 3, 4]
v =
1 2 3 4
31
Row Vectors
• MATLAB workspace after a row vector has been assigned:
32
Row Vectors: Colon Operator
• The colon operator (:) can be used to iterate values in a vector
• Format:
Vector_variable_name = initial value : step: final value
step size
• Example 1:
v =
1 3 5 7
33
Row Vectors: Colon Operator
• Example 2:
>> v = 1:6 If “step” is omitted, the default step size is 1
v =
1 2 3 4 5 6
34
Row Vectors: Colon Operator
• Example 3:
>> v = 1:3:12
v =
35
Row Vectors: Colon Operator
• Example 4:
v =
Observe that the final value is not
20 17 14 11 8 guaranteed to be at the end of the
vector
36
Row Vectors: Linspace
• linspace(x,y,n) function creates a vector with n values in the inclusive
range from x to y. It creates a vector with n values that are linearly
spaced.
37
Concatenating the vectors
• Vectors can be created by joining together existing vectors, or adding
elements to existing vectors
• Example :
>> v1 = 2:5;
>> v2 = 1:3;
>> v_total = [v1 v2]
v_total =
2 3 4 5 1 2 3
From v1 From v2
38