Lecture Note #3
Lecture Note #3
2
MATLAB Basic Functions – Clc & Clear
“clc” function can be used to clear the command window. It can be written
as follows:
>> clc
Or
>> clc()
Another function which is frequently used is “clear”, it is used to clear the
entire workspace variables or a specific variable.
To delete all variables:
>> clear
To delete a specific variable(s):
>> clear variable name
Where:
variable name: is the name of the variable(s) to be cleared.
3
MATLAB Basic Functions – Command window format style
“format” function controls the output format. It can be written as follows:
>> format style
Where:
style: can be any of the following pre-defined styles in MATLAB
4
MATLAB Basic Functions – Sum
“sum” function allows the user to add the value of elements within a
matrix. It can be written as follows:
To add all column elements:
>> sum(a) Or >> sum(a,1)
To add all row elements:
>> sum(a,2)
To add all elements in the matrix:
>> sum(sum(a))
Where:
a: is a variable name (scalar, vector, or matrix).
5
MATLAB Basic Functions –Prod
“prod” function allows the user to multiply the value of elements within a
matrix. It can be written as follows:
To multiply all column elements:
>> prod(a) Or >> prod(a,1)
To multiply all row elements:
>> prod(a,2)
To multiply all elements in the matrix:
>> prod(prod(a))
Where:
a: is a variable name (scalar, vector, or matrix).
6
MATLAB Basic Functions –Abs & Sqrt
7
MATLAB Basic Functions – Max
“max” function outputs the maximum value in a vector. It can be written as
follows:
To find the maximum value of a vector:
>> max(a)
To find the maximum value in each column or row of a matrix:
For column max: >> max(b) For row max: >> max(b')
To find the absolute maximum value of a matrix:
>> max(max(b))
To replaced all the numbers which are smaller than a specified number by
that number:
For vectors: >> max(a,c) For matrices: >> max(b,c)
Where:
a: is a vector name. b: is a matrix name. c: specified min number.
8
MATLAB Basic Functions – Min
“min” function outputs the minimum value in a vector. It can be written as
follows:
To find the minimum value of a vector:
>> min(a)
To find the minimum value in each column or row of a matrix:
For column min: >> min(b) For row min: >> min(b')
To find the absolute minimum value of a matrix:
>> min(min(b))
To replaced all the numbers which are larger than a specified number by
that number:
For vectors: >> min(a,c) For matrices: >> min(b,c)
Where:
a: is a vector name. b: is a matrix name. c: specified max number.
9
MATLAB Basic Functions – Mean
“mean” function outputs the mean value of a vector or matrix. It can be
written as follows:
To find the mean value of each column:
>> mean(a) Or >> mean(a,1)
To find the mean value of each row:
>> mean(a,2)
To find the overall mean value of a matrix:
>> mean(mean(a))
Where:
a: is a variable name (scalar, vector, or matrix).
10
MATLAB Basic Functions – Median
“median” function outputs the median value of a vector or matrix. It can be
written as follows:
To find the median value of each column:
>> median(a) Or >> median(a,1)
To find the median value of each row:
>> median(a,2)
To find the overall median value of a matrix:
>> median(median(a))
Where:
a: is a variable name (scalar, vector, or matrix).
Note:
• For odd sized sample: the median value is the middle value.
• For even sized sample: the median is the average of the two middle
values.
11
MATLAB Basic Functions – Mode
“mode” function outputs the number which is the repeated the most within a
vector or matrix. It can be written as follows:
To find the mode value of each column:
>> mode(a) Or >> mode(a,1)
To find the mode value of each row:
>> mode(a,2)
To find the overall mode value of a matrix:
>> mode(mode(a))
Where:
a: is a variable name (scalar, vector, or matrix).
Note:
• If no number is repeated: the mode function will give the first number
which appears in the variable.
• If two or more numbers are repeated the most: the mode will be the
first number which appeared in the variable.
12
MATLAB Basic Functions – Var
“var” function outputs the variance value of a vector or matrix. It can be
written as follows:
To find the variance value of each column:
>> var(a) Or >> var(a,1)
To find the variance value of each row:
>> var(a,2)
To find the overall variance value of a matrix:
>> var(a(:))
Where:
a: is a variable name (scalar, vector, or matrix).
Note:
15
MATLAB Basic Functions – Rand
“rand” function creates a scalar or an array with a selected size containing
random values ranging between 0 and 1. It can be written as follows:
To generate a single random value between 0 and 1:
>> rand Or >> rand() Or >> rand(1)
To generate a square sized matrix containing random values between 0 and
1:
>> rand(a) Or >> rand(a,a)
To generate a non-square sized matrix containing random values between 0
and 1:
>> rand(a,b)
Where:
a and b: are two unequal integers.
16
MATLAB Basic Functions – Randi
“randi” function creates a scalar or an array with a selected size containing
random integers within a specified range. It can be written as follows:
To generate a single random integer ranging between 0 to a:
>> randi(a)
To generate a single random integer ranging between a to b:
>> randi([a,b])
To generate a square sized matrix containing random integers ranging
between a to b:
>> randi([a,b],c)
To generate a non-square sized matrix containing random integers ranging
between a to b:
>> randi([a,b],c,b)
Where:
a, b, c and d: are all integers, a & b are not equal and c & d are not equal.
17
MATLAB Basic Functions – Linspace
“linspace” function creates a vector with specified size, and starting &
ending values where the vector elements will be linearly spaced between
the specified values. It can be written as follows:
To generate a row vector of 100 linearly equally spaced points between a
and b:
>> linspace(a,b)
To generate a row vector of a specified sized (c) where the values are
linearly equally spaced between a and b:
>> linspace(a,b,c)
Where:
a and b: are any real numbers.
c: is the number of columns in the produced vector. The linear increment is
𝑏𝑏−𝑎𝑎
always equal to .
𝑐𝑐−1
18
MATLAB Basic Functions – Round & floor
“round” function rounds a scalar or all the elements of an array towards the
nearest integer. It can be written as follows:
To round the values saved in a variable named a:
>> round(a)
Where:
a: is a variable name (scalar, vector, or matrix).
“floor” function rounds a scalar or all the elements of an array towards the
nearest integer towards −∞. It can be written as follows:
To round the values saved in a variable named a towards minus infinity:
>> floor(a)
Where:
a: is a variable name (scalar, vector, or matrix).
19
MATLAB Basic Functions – Ceil & fix
“ceil” function rounds a scalar or all the elements of an array towards the
nearest integer towards ∞. It can be written as follows:
To round the values saved in a variable named a towards infinity:
>> ceil(a)
Where:
a: is a variable name (scalar, vector, or matrix).
“fix” function rounds a scalar or all the elements of an array towards the
nearest integer towards 0. It can be written as follows:
To round the values saved in a variable named a towards zero:
>> fix(a)
Where:
a: is a variable name (scalar, vector, or matrix).
20
MATLAB Basic Functions – Mod
“mod” function finds the modulus after division of a scalar or all the
elements of an array. It can be written as follows:
To find the modulus after dividing a variable named (a) by a variable named
(b):
>> mod(a,b)
Where:
a: is a variable name (scalar, vector, or matrix).
b: is a variable name it can either be a scalar or an array with a size matching
with variable a.
Note:
If the value of the divisor b is set as 0
or a value larger than a, the output
would simply be a.
21
MATLAB Basic Functions – Pi & Exp
“pi” function can be used to obtain pi value. It can be written as follows:
To find obtain pi value:
>> pi Or >> pi()
“exp” function can be used to obtain e value raised to any specified power.
The power can either be a scalar or each element of an array. It can be
written as follows:
To obtain the result of e raised to the power of each element in variable
(a):
>> exp(a)
Where:
a: is a variable name (scalar, vector, or matrix).
22
MATLAB Basic Functions – Log, Log10, & Log2
“log”, “log10”, and “log2” functions can be used to obtain base-e, base-10,
and base-2 logarithm of a specified variable respectively. The variable can
either be a scalar or an array where the log of each element will be
calculated. It can be written as follows:
Note:
To obtain the base-e logarithm of variable (a):
The expressions used to find Log of the
>> log(a) three bases are as shown below:
𝒍𝒍𝒍𝒍(𝒂𝒂)
To obtain the base-10 logarithm of variable (a): • Base-e: 𝒍𝒍𝒍𝒍𝒍𝒍(𝒂𝒂) =
𝒍𝒍𝒍𝒍(𝒆𝒆)
𝒍𝒍𝒍𝒍(𝒂𝒂)
>> log10(a) • Base-10: 𝒍𝒍𝒍𝒍𝒍𝒍𝒍𝒍𝒍𝒍(𝒂𝒂) =
𝒍𝒍𝒍𝒍(𝟏𝟏𝟏𝟏)
𝒍𝒍𝒍𝒍(𝒂𝒂)
To obtain the base-2 logarithm of variable (a): • Base-2: 𝒍𝒍𝒍𝒍𝒍𝒍𝒍𝒍(𝒂𝒂) =
𝒍𝒍𝒍𝒍(𝟐𝟐)
>> log2(a)
Where:
a: is a variable name (scalar, vector, or matrix). It can be any positive number
greater than 0. It can either be a number or cell name.
23
MATLAB Basic Functions – Sin & Asin
“sin” function finds the sine value of a scalar or all the elements of an
array. The input value has to be in radians. It can be written as follows:
To find the sine of a variable named (a):
>> sin(a)
Graph
Where:
a: is a variable name (scalar, vector, or matrix).
“asin” function finds the arc-sine value of a scalar or all the elements of an
array. The input value has to be between -1 and 1 and the corresponding
−𝜋𝜋 𝜋𝜋
output value will be between and . It can be written as follows:
2 2
To find the arc-sine of a variable named (a):
>> asin(a)
Where:
a: is a variable name (scalar, vector, or matrix).
24
MATLAB Basic Functions – Cos & Acos
“cos” function finds the cosine value of a scalar or all the elements of an
array. The input value has to be in radians. It can be written as follows:
To find the cosine of a variable named (a):
>> cos(a)
Graph
Where:
a: is a variable name (scalar, vector, or matrix).
“acos” function finds the arc-cosine value of a scalar or all the elements of
an array. The input value has to be between -1 and 1 and the corresponding
output value will be between 𝜋𝜋 and 0. It can be written as follows:
To find the arc-cosine of a variable named (a):
>> acos(a)
Where:
a: is a variable name (scalar, vector, or matrix).
25
MATLAB Basic Functions – Tan & Atan
“tan” function finds the tangent value of a scalar or all the elements of an
array. The input value has to be in radians. It can be written as follows:
To find the tangent of a variable named (a):
>> tan(a)
Graph
Where:
a: is a variable name (scalar, vector, or matrix).
“atan” function finds the arc-tangent value of a scalar or all the elements
of an array. The input value can be any real number and the corresponding
−𝜋𝜋 𝜋𝜋
output value will be between and . It can be written as follows:
2 2
To find the arc-tangent of a variable named (a):
>> atan(a)
Where:
a: is a variable name (scalar, vector, or matrix).
26
MATLAB Basic Functions – Sinh & Asinh
“sinh” function finds the hyperbolic sine value of a scalar or all the
elements of an array. The input value has to be in radians. It can be written
as follows:
To find the hyperbolic sine of a variable named (a):
>> sinh(a)
Graph
Where:
a: is a variable name (scalar, vector, or matrix).
“asinh” function finds the arc-sine value of a scalar or all the elements of
an array. The input value can be any real number and the corresponding
output value will be between −∞ and ∞. It can be written as follows:
To find the inverse of hyperbolic sine of a variable named (a):
>> asinh(a)
Where:
a: is a variable name (scalar, vector, or matrix).
27
MATLAB Basic Functions – Cosh & Acosh
“cosh” function finds the hyperbolic cosine value of a scalar or all the
elements of an array. The input value has to be in radians. It can be written
as follows:
To find the hyperbolic cosine of a variable named (a):
>> cosh(a)
Graph
Where:
a: is a variable name (scalar, vector, or matrix).
“acosh” function finds the arc-cosine value of a scalar or all the elements
of an array. The input value can be any real number and the corresponding
output value will be between −∞ and ∞. It can be written as follows:
To find the inverse of hyperbolic cosine of a variable named (a):
>> acosh(a)
Where:
a: is a variable name (scalar, vector, or matrix).
28
MATLAB Basic Functions – Tanh & Atanh
“tanh” function finds the hyperbolic tangent value of a scalar or all the
elements of an array. The input value has to be in radians. It can be written
as follows:
To find the hyperbolic tangent of a variable named (a):
>> tanh(a)
Graph
Where:
a: is a variable name (scalar, vector, or matrix).
“atanh” function finds the arc-tangent value of a scalar or all the elements
of an array. The input value has to be between -1 and 1 and the
corresponding output value will be between −∞ and ∞. It can be written as
follows:
To find the inverse of hyperbolic tangent of a variable named (a):
>> atanh(a)
Where:
a: is a variable name (scalar, vector, or matrix).
29
MATLAB Basic Functions – Rad2deg & Deg2rad
“rad2deg” function converts a scalar or all the elements of an array from
radians to degrees. It can be written as follows:
To convert the elements of a variable named (a) from radians to degrees:
>> rad2deg(a) Note:
31
MATLAB Basic Functions – Length
“length” function finds the length of a scalar or an array. It can be written
as follows:
To find the length of a variable named (a):
>> length(a)
Where:
a: is a variable name (scalar, vector, or matrix).
Note:
• The length considers the larger dimension of the selected
array. It can either be the number of rows or number of
columns depending on the size.
• The length can also be expressed as follows:
length(a) = max(size(a))
32
MATLAB Basic Functions – Numel
“numel” function finds the number of elements that exist in a scalar or an
array. It can be written as follows:
To find the number of elements of a variable named (a):
>> numel(a)
Where:
a: is a variable name (scalar, vector, or matrix).
Note:
Numel can also be expressed
as follows:
numel(a) = length(a(:))
33
MATLAB Basic Functions – Sort
“sort” function outputs the input variable where the elements are sorted in
ascending or descending order. It can be written as follows:
To sort the values in each column:
>> sort(a,b*) Or >> sort(a,1,b*)
To sort the values in each row:
>> sort(a,2,b*)
To sort all the elements in a matrix:
>> sort(a(:),b*)
Where:
a: is a variable name (scalar, vector, or matrix).
b: is an optional argument (if not input, MATLAB will assume the sorting to be
done in ascending order). It is used to set the type of sorting required from the
function: for ascending order the user must write ‘ascend' and for descending
order the user must type 'descend'.
34
MATLAB Basic Functions – Disp & Save*
“disp” function displays all the elements in an existing variable. It can be
written as follows:
To display the elements of a variable named (a):
>> disp(a)
Where:
a: is a variable name (scalar, vector, or matrix).
36
MATLAB Basic Functions – Logical conditions
It is possible to set a logical condition in MATLAB simply by typing it in the
command window.
The logical condition must contain any of the operators mentioned in the
following table:
Operator Condition
a==b The value of a equals the value of b
a>b The value of a is larger than the value of b
a>=b The value of a is larger than or equal to the value of b
a<b The value of a is smaller than the value of b
a<=b The value of a is smaller than or equal to the value of b
a~=b The value of a does not equal the value of b
37
MATLAB Basic Functions – Logical conditions
It is possible to set multiple logical conditions in MATLAB where the user
can specifies whether or not all or any of the logical conditions must be
satisfied. The operators used for each purpose are shown below:
Expression Operator Condition
Both conditions must be satisfied (true) to satisfy the
And (Condition 1) & (Condition 2)
logical condition.
At least one of conditions must be satisfied (true) to
or (Condition 1) | (Condition 2)
satisfy the logical condition.
Not ~(Condition) The condition is not satisfied
38
MATLAB Basic Functions – Logical conditions
It is important to note that the logical conditions can be used to verify all
the values in an array. In order to do so, one of the following conditions
must be met:
In MATLAB 2014 In MATLAB 2017 and newer versions
• The size of the variables mentioned in the • The size of the variables mentioned in the
condition must be an exact match. condition must be an exact match.
• The size of one of the variables mentioned • The number of row or number of columns
in the condition must be a scalar. of both variables mentioned in the
condition must match.
• The size of one of the variables mentioned
in the condition must be a scalar.
It is useful to think of OR (|) as an addition of the logical results obtained
by each of the conditions. Where the addition result cannot exceed 1 (any
value larger than 1 is kept as 1).
As for AND (&), it can be considered as a multiplication of the logical results
obtained by each of the conditions. Where the multiplication result would
only be 1 if all the parallel logical results are 1, otherwise it would be 0.
39
MATLAB Basic Functions – Logical conditions
Example: write down the expected output generated from the following
logical conditions:
a=[1 2 3;4 5 6;7 8 9]; b=[3 2 1];
1. a>=4
Ans = [0 0 0;1 1 1;1 1 1]
2. a==b
Ans = [0 1 0;0 0 0;0 0 0]
3. a(1,:)==b
Ans = [0 1 0]
4. a(1,:)~=b & a>=4
Ans = [0 0 0;1 0 1;1 0 1]
5. a(1,:)~=b & a(2,:)>=4
Ans = [1 0 1]
40
MATLAB Basic Functions – Logical conditions
Example: write down the expected output generated from the following
logical conditions:
a=[1 2 3;4 5 6;7 8 9]; b=[3 2 1];
6. a(1,:)~=b | a(2,:)>=4
Ans = [1 1 1]
7. a(1,:)~=b | a(2,:)>=4 & a(3,:)>8
Ans = [1 0 1]
8. (a(1,:)~=b | a(2,:)>=4) & a(3,:)>8
Ans = [0 0 1]
9. (a(1,:)~=b | a(2,:)>=4) & (a(3,:)>8 | a(3,:)<9)
Ans = [1 1 1]
41
MATLAB Basic Functions – Find
“find” function finds the indices of non-zero values present in a variable. It
can also be used to find the indices of the values that satisfy certain
condition(s). It can be written as follows:
To find the indices of non zero values in a variable:
>> find(a)
To find the indices of the values that satisfy certain condition(s):
>> find(condition)
Where:
a: is a variable name (scalar, vector, or matrix).
Condition: is the condition to verified.
Example: find the indices of positive numbers in array (a)
Command Output
ans =
>> a=[1 2 3;-5 -6 -8] 1
>> find(a>0) 3
5 42
MATLAB Basic Functions – Sum with condition
It is possible to use “sum” function to add the output obtained after
checking a logical condition. This would subsequently count the number
elements that satisfied the set condition(s) (since the output of logical
condition would either be 0 or 1).
To count the number of elements that satisfied certain condition(s):
>> sum(sum(condition))
Where:
Condition: is the condition to verified.
Example: find the indices of positive numbers in array (a)
Command
>> a=[1 2 3;-5 -6 -8]
>> sum(sum(a>0))
Output
ans = 3
43
MATLAB Basic Functions – Practice
Practice:- using the learned MATLAB functions write down the command(s)
used to obtain the following:
1. 1:10
>> linspace(1,10,10)
2. The position of 6 in a variable named a.
>> find(a==6)
3. The number of elements existing in variable a that are larger than 1 but not
more than 10.
>> sum(sum(a>1 & a<=10))
4. The average of positive values in variable named a.
>> mean(a(find(a>0)))
5. The percentage of positive numbers existing in variable named a.
>> sum(sum(a>0))/numel(a)*100
44