SlideShare a Scribd company logo
MATLAB TUTORIAL 2
By Norhan Abdalla
Outline:
■ Built In Functions
■ Complex Numbers
■ Matrices Algebra
■ Plotting
BUILT-IN FUNCTIONS
Math Functions
■ abs(x) Finds the absolute value of x
■ sqrt(x)Finds the square root of x
■ nthroot(x,n)Finds the real nth root of x.
■ sign(x)Returns 0 if (x=0) ,returns 1 if (x>0) ,or returns -1 if (x<0)
■ rem(x,y)Computes the remainder of x/y
■ exp(x)Computes the value of 𝑒𝑥
■ log(x)Computes ln(x) –the natural logarithm –
■ log10(x)Computes 𝑙𝑜𝑔10(𝑥) –the common logarithm –
This
function will
not return
complex
results
The e
notation and
exp(x) are
not the
same.
Practice:
■ Define this vector x1 = [ -2 ,-1 ,0 , 1 , 2]
A. Find the absolute value of each element of the vector.
B. Find the square root of each element of the vector.
■ Use the ‘nthroot’ function to find the square root of -3 and 3.
■ Create a vector x2 from -9 to 12 with an increment of 3.
A. Find ln(x) –the natural logarithm –
B. Find Log10(x) –the common logarithm –
■ Use the sign function to determine which elements in x2 are positive.
Rounding Functions
■ round(x) Rounds x to the nearest integer
■ fix(x)Rounds /truncates x to the nearest integer toward zero
■ floor(x) Rounds x to the nearest integer toward negative infinity
■ ceil(x)Rounds x to the nearest integer toward positive infinity
Discrete Mathematics
■ factor(x)Finds the prime factors of x
■ gcd(x,y)Finds the greatest common divisor of x and y
■ lcm(x,y)Finds the least common multiple of x and y
■ rats(x)Represents x as a fraction
■ factorial(x)Finds the value of x!
Discrete Mathematics
■ nchoosek(n,k)Finds the number of possible combinations
of k items from a group of n items.
❖ Hint:
• This instruction is used for combinations ( 𝐶(𝑛,𝑟) =
𝑛!
𝑟! 𝑛−𝑟 !
)
• For permutations (𝑃(𝑛,𝑟) =
𝑛!
𝑛−𝑟 !
)
you can use factorial(n) /factorial(n-r)
■ primes(x) Finds all the prime numbers less than x
■ isprime(x)Checks to see if x is a prime number.
Practice:
■ Factor the number 322.
■ Find the greatest common divisor of 322 and 6 .
■ Is 322 a prime number?
■ Find the number of possible groups containing 3 people from a group of
29,when order does not matter.
Trigonometric Functions
■ sin(x)Finds the sin of x when x is in Radians
■ asin(x)Finds the arcsin/ inverse of sine of x when x =[-1,1]
• The function returns an angle in radians Ɵ=[−
ᴨ
2
,
ᴨ
2
]
■ sinh(x)Finds the hyperbolic sine of x when x is in radians
■ asinh(x)Finds the inverse of the hyperbolic sin of x
■ sind(x)Finds the sin of when x is expressed in degrees
■ asind(x)Finds the inverse sin of x when x=[-1,1]
• The function returns the angle in degrees
You can use
similar
functions in
cos(x), tan(x)
Practice:
■ sin(2Ɵ) for Ɵ = 3ᴨ
■ Cos(Ɵ) for 0≤ 2ᴨ ; let Ɵ change in steps of 0.2ᴨ
■ 𝑠𝑖𝑛−1
(1)
■ 𝑐𝑜𝑠−1
𝑥 for -1≤ x ≤ 1; let Ɵ change in steps of 0.2
■ Find the cosine of 45°
■ Find the angle whose sine is 0.5. Is your answer in degrees or radians?
■ Find the cosecant of 60.
Calculate the following :
Data Analysis Functions
Maxima & Minima
■ max(x) Finds the largest value in each
column.
❖ Hint:
• Using the transpose extracts the maximum of
each row, max(x’)
■ [a,b] = max(x)Assigns the maximum
elements in each column to a
and assigns their locations to b
■ max(x,y)Creates a x*y matrix, then
compares between the two vectors
You can use
similar
functions for
minima.
Use “min”
Practice:
• Create the following matrix:
x= [4,90,85,75;2,55,65,75;3,78,82,79;1,84,92,93]
■ What is the maximum value in each column?
■ In which row does that maximum occur?
■ What is the maximum value in each row?
■ In which column does the maximum occur?
■ What is the maximum value in the entire table?
Data Analysis Functions
Averages
■ mean(x) Finds the mean value of each
column of the matrix.
❖ Hint:
• mean(mean(x))Finds the mean value of
matrix x
■ median(x)Finds the median value in each
column
■ mode(x)Finds the value that occurs most
often in the array
Practice:
• Define the following matrix:
x= [4,90,85,75;2,55,65,75;3,78,82,79;1,84,92,93]
■ What is the mean value in each column?
■ What is the median value in each column?
■ What is the mean value in each row?
■ What is the median value in each row?
■ What is returned when you call “ mode” function?
■ What is the mean for the entire matrix?
Data Analysis Functions
Sums & Products
■ sum(x)Sums the elements in each
column
■ prod(x) Computes the product of each
column
■ cumsum(x)Computes the cumulative
sums of the elements
■ cumprod(x) Computes the cumulative
products of the elements
❖ Hint:
• sum(x(:,1)) Finds the sum of all the
rows in the first column of matrix
• sum(x(1,:)) Finds the sum of all the
columns in the first row of matrix x
Data Analysis Functions
Sorting
■ sort(x)Sorts the matrix column wise in
ascending order
■ sort(x,’descend’)Sorts the elements in
each column in descending order
■ sortrows(x) Sorts the matrix row wise
in ascending order on the bases of
column 1( keeps the row intact)
■ sortrows(x,n)Sorts the rows based on
the nth column, but maintains the rows
unchanged.
• If n is negative , the values are sorted in
descending order.
• If n is not specified , the default column
is 1
Data Analysis Functions
Size
■ size(x)Determines the number of rows
and columns in matrix x
■ [a,b]= size(x) Determines the number
of rows and columns in matrix x then
assigns rows to a and columns to b
■ length(x)Determines the number of the
largest dimension in a matrix
■ numel(x)Determines the total number
of elements in a matrix x
Practice:
• define the following matrix:
x=
[4,90,85,75;2,55,65,75;3,78,82,79;1,84,92,93]
■ Use the size function to determine the
number of rows and columns in this matrix.
■ Use the sort function to sort each column in
ascending order.
■ Use the sort function to sort each column in
descending order.
■ Use the sortrows function to sort the matrix
so that the first column is in ascending order.
■ Use the sortrows function to sort the matrix
from the 4th exercise in descending order;
based on the third column.
Data Analysis Functions
Statistical Functions
■ std(x) Computes the standard deviation of the
values in a vector x.
■ var(x) Calculates the variance of x.
• For 2d matrix use the operator (:)
■ std(x(:)) Computes the standard deviation of all
the values in a matrix x
■ var(x(:)) Calculates the variance of all the
values in a matrix x
Practice:
• Define the following matrix:
x= [4,90,85,75;2,55,65,75;3,78,82,79;1,84,92,93]
■ Find the standard deviation for each column.
■ Find the variance for each column.
■ Calculate the square root of the variance you
found for each column.
Special Functions
■ i/j Means imaginary number
■ Inf Means infinity , which occurs during a
calculational overflow or when a number is
divided by zero
■ NaN Means not a number, occurs when a
calculation is undefined
■ clock Returns current time
■ date Returns current date
COMPLEX NUMBERS
Complex Numbers
■ abs(x)Computes the absolute value of a
complex
■ angle(x)Computes the angle from the
horizontal in radius
■ complex(x,y) Generates a complex number
with a real component x and an imaginary
component y
■ conj(x)Generates the complex conjugate of a
complex number
■ imag(A) Extracts the imaginary component
of A
■ real(A) Extracts the real component of A
imaginary
real
Practice:
■ Create the following complex numbers:
A= 1+j
B=2-3j
C= 8+2j
■ Create a vector D of complex numbers whose real components are 2,4
and 6 and whose imaginary components are -3 , 8 and -16.
■ Find the magnitude (absolute value) of each of the vectors you created
above.
■ Use the transpose operator to find the complex conjugate of vector D.
MATRICES ALGEBRA
Dot & Cross Product:
■ Dot Product : vector * vector = scalar
■ Cross Product: vector * vector= vector
• Remember:
A* B ≠ B* A
Practice:
• Define the following vectors:
• Ԧ
𝐴 = [ 1, 2, 3, 4 ] , 𝐵 = [ 12, 20 ,15, 7 ]
• Find the dot product of Ԧ
𝐴 .𝐵
A & B must
have the
same
number of
elements
Matrix Inverse & Determinant
■ What is an inverse of a matrix?
• It is the matrix when you multiply it by the original
matrix you get an identity matrix
• Identity matrix : is a matrix consists of ones down the
main diagonal and zeros in all the other locations
■ In order for a matrix to have an inverse , it has to be a
square matrix (m*m)
■ When a matrix has an inverse ,it is called an invertible
matrix
■ When a matrix has no inverse ,it is called a singular
matrix
■ To get the inverse of a matrix ,the determinant should
not be 0
■ If the determinant of a matrix is 0, the matrix has no
inverse
Special Matrices
■ diag(A) Extracts the diagonal of
a two dimensional matrix A
■ fliplr(A) Flips a matrix into its
mirror image
■ flipud(A) Flips a matrix
vertically
■ magic matrix : is a matrix with
unusual properties.
• The sum of columns is the same as
the sum of rows and also the sum
of diagonals.
• The sum of the diagonal line is
equal to the sum of the diagonal
line after flipping it.
• Here is the
proof
• Try : diag(A, 1)
Special Matrices
■ Identity matrix:
• eye (m), eye(m,n)
NOTE:
• Do not name an identity matrix ‘i’
,because ‘i’ will no longer represent
−1 in any statements that follow
■ all  Determines if all the
elements are non zero
■ any  Determines if any element
is non zero
■ ones(m,n,z) Creates a 3 D matrix
with m rows, n columns , z pages
Special Matrices
■ expm(A) Returns matrix A’s
exponential
■ sqrtm(A) Returns the square root
of matrix A
■ eig(A) Returns eigenvalues and
eigenvectors of matrix A
PLOTTING
Basic Plotting
■ Try these commands:
• x= [ 0:2:18];
• y = exp(x)-3;
• plot(x,y)
• title(‘ lab experiment ‘)
• xlabel(‘ x axis ’)
• ylabel(‘ y axis ’)
• grid on
create a graph first ,
then add title and
axis labels.
Plotting :
Labeling:
Basic Plotting
■ plot Creates an x-y plot
■ title(‘TEXT’) Adds title to a plot
■ xlabel(‘TEXT’)  Adds a label to
the x axis
■ ylabel (‘TEXT’) Adds a label to
the y axis
■ grid Adds a grid to the graph
■ pause  Pauses the execution of the
program ,allowing the user to view
the graph
■ figure Determines which figure
will be used for the current plot
■ hold Freezes the current plot , so
that an additional plot can be
overlaid.
Multiple plots
■ To plot two functions on
the same window:
■ To use new window to plot a
new graph :
• Use figure(#)
Plotting a Matrix
■ Consider the following code:
■ If the plot contains a matrix ,
MATLAB does a separate line
for each column, and the x-axis
becomes the row vector
Subplots
■ The ‘subplot’ command allows
you to subdivide the graphing
window into a grid of m rows
and n columns.
• subplot(m,n,p)
No. of rows
No. of columns
location
Practice:
■ Subdivide a figure window into two rows and one column.
■ In the top window ,plot y =tan(x) for
-1.5 ≤ x ≤ 1.5 (use an increment of 1).
■ Add a title and axis labels to your graph.
■ In the bottom window ,plot y =sinh(x) for the same range.
■ Add a title and labels to your graph.
■ Try this exercise again ,but divide the figure window vertically instead of
horizontally.
Other Functions
■ To generate a square wave :
• Use square ( 𝜔 *t , rho)
■ To generate a triangular wave :
• Use sawtooth(𝜔 *t , rho)
■ Polar Plots:
• Use polar(theta, r)
Practice:
*Hint : Use the command: polar(theta, r)
■ Declare theta from 0 to 2pi ,in steps of 0.01*pi.
1. Define an array r= 5*cos(4*theta), then plot it in polar form.
2. Define an array r= 4*cos(6*theta), then plot it in polar form.
3. Define an array r= 5 –5*sin(theta), then plot it in polar form.
4. Define an array r= sqrt(5^2*cos(2*theta)), then plot it in polar form.
■ Declare theta = pi/2:4/5*pi:4.5pi
• Define an array r which is a six-member array of ones ,then plot it in a
polar form.
Editing the Graph
■ Changing the shape of line , mark and style.
• They come in this order: ( ‘ line, point type , color’)
■ Axis scaling:
• axis([ x,min x,max y,min y,max])
Max of y-axis
Max of x-axis
Min of x-axis
Min of y-axis
Dotted line
Circled points
Black color
Line, Mark , and Color Options :
Line Type Indicator Point Type Indicator Color Indicator
solid - point . blue b
dotted : circle o green g
dash-dot -. x-mark x red r
dashed - - plus + cyan c
star * magenta m
square s yellow y
diamond d black k
triangle down v white w
triangle up ^
triangle left <
triangle right >
pentagram p
hexagram h
Editing the Graph
■ axis(v) v is the input to axis command , it
must be 4 element vector
■ axis equal  Forces the scaling on the x and y
axis to be the same
■ legend(‘ string1 , ‘string2’) Allows you to add
legends to your graph
■ text(x_coordinate , y_coordinate , ‘TEXT’)
Adds a text at point (x,y)
■ gtext(‘ string’) Similar to the ‘text’ command ,
but the location is determined by the user by
clicking in the figure window manually.
Pie and Bar Graphs
■ Try the following:
Pie and Bar Graphs
■ bar(x) When x is a vector , ‘bar’ generates a vertical bar graph and
when x is 2d matrix ,bar groups the data by row
■ barh(x) Similar to bar , but generates horizontal bars
■ bar3(x) Generates a 3d bar chart
■ barh3(x) Generates a 3d horizontal bar chart
■ pie(x) Generates a pie chart. Each element of the matrix is
represented as a slice of a pie
■ pie3(x) Generates a 3d pie chart.

More Related Content

PPT
Tutorials--Graphs of Rational Functions
PDF
Math 4 lecture on Graphing Rational Functions
PPTX
Graphing rational functions
KEY
PPT
Rational Functions
PPTX
Rational function representation
PPT
9.3 Intro to Rational Functions
Tutorials--Graphs of Rational Functions
Math 4 lecture on Graphing Rational Functions
Graphing rational functions
Rational Functions
Rational function representation
9.3 Intro to Rational Functions

What's hot (19)

PPT
Rational Function
PPTX
Systems of Equations Lecture
PDF
Matlab practice
PPTX
Graphing rational functions
PDF
3.5 Rational Functions
PPT
M17 t1 notes
PPTX
Matrix2 english
PPT
Rational functions 13.1 13.2
PPTX
Algebraic Properties of Matrix Operations
PPTX
Rational functions
PPT
Graphing rational functions
PPTX
Matrix algebra
PPT
Rational functions
PPT
Polynomial and thier graphs
PPT
Graphing, Slope, And Special Lines
PPTX
matrix algebra
PPT
Rational, Irrational & Absolute Value Functions Review
PPTX
Bba i-bm-u-2- matrix -
PDF
General Mathematics - Rational Functions
Rational Function
Systems of Equations Lecture
Matlab practice
Graphing rational functions
3.5 Rational Functions
M17 t1 notes
Matrix2 english
Rational functions 13.1 13.2
Algebraic Properties of Matrix Operations
Rational functions
Graphing rational functions
Matrix algebra
Rational functions
Polynomial and thier graphs
Graphing, Slope, And Special Lines
matrix algebra
Rational, Irrational & Absolute Value Functions Review
Bba i-bm-u-2- matrix -
General Mathematics - Rational Functions
Ad

Similar to Matlab tutorial 2 (20)

PPTX
introduction to matlab.pptx
PPTX
Introduction to Matlab and application.pptx
PPTX
Introduction to MATLAB Programming for Engineers
PPTX
Introduction to matlab
PPTX
Linear Algebra Presentation including basic of linear Algebra
PPTX
presentation.pptx
PPT
Introduction to Matlab - Basic Functions
PDF
chapter1.pdf ......................................
PPTX
Strassen's Matrix Multiplication divide and conquere algorithm
PDF
Mit6 094 iap10_lec03
PPTX
INTRODUCTION TO MATLAB presentation.pptx
DOCX
1.Select the graph of the quadratic function ƒ(x) = 4 – x2. Iden.docx
PPT
Matrix Algebra : Mathematics for Business
PPTX
MATLABgraphPlotting.pptx
PDF
Matlab tutorial 1
PDF
Applied Mathematics 3 Matrices.pdf
PDF
PPT
Machine Learning Unit 2_Supervised Learning
PPT
G9-Math-Q1-Week-9-Solving-Quadratic-Function.ppt
PPTX
Linear Algebra in python programming....
introduction to matlab.pptx
Introduction to Matlab and application.pptx
Introduction to MATLAB Programming for Engineers
Introduction to matlab
Linear Algebra Presentation including basic of linear Algebra
presentation.pptx
Introduction to Matlab - Basic Functions
chapter1.pdf ......................................
Strassen's Matrix Multiplication divide and conquere algorithm
Mit6 094 iap10_lec03
INTRODUCTION TO MATLAB presentation.pptx
1.Select the graph of the quadratic function ƒ(x) = 4 – x2. Iden.docx
Matrix Algebra : Mathematics for Business
MATLABgraphPlotting.pptx
Matlab tutorial 1
Applied Mathematics 3 Matrices.pdf
Machine Learning Unit 2_Supervised Learning
G9-Math-Q1-Week-9-Solving-Quadratic-Function.ppt
Linear Algebra in python programming....
Ad

Recently uploaded (20)

PPTX
Geodesy 1.pptx...............................................
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Well-logging-methods_new................
DOCX
573137875-Attendance-Management-System-original
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
web development for engineering and engineering
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Welding lecture in detail for understanding
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPT
Mechanical Engineering MATERIALS Selection
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
Construction Project Organization Group 2.pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
Geodesy 1.pptx...............................................
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
UNIT 4 Total Quality Management .pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Lecture Notes Electrical Wiring System Components
Internet of Things (IOT) - A guide to understanding
Operating System & Kernel Study Guide-1 - converted.pdf
Well-logging-methods_new................
573137875-Attendance-Management-System-original
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
web development for engineering and engineering
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Welding lecture in detail for understanding
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Mechanical Engineering MATERIALS Selection
R24 SURVEYING LAB MANUAL for civil enggi
Construction Project Organization Group 2.pptx
CYBER-CRIMES AND SECURITY A guide to understanding

Matlab tutorial 2

  • 1. MATLAB TUTORIAL 2 By Norhan Abdalla
  • 2. Outline: ■ Built In Functions ■ Complex Numbers ■ Matrices Algebra ■ Plotting
  • 4. Math Functions ■ abs(x) Finds the absolute value of x ■ sqrt(x)Finds the square root of x ■ nthroot(x,n)Finds the real nth root of x. ■ sign(x)Returns 0 if (x=0) ,returns 1 if (x>0) ,or returns -1 if (x<0) ■ rem(x,y)Computes the remainder of x/y ■ exp(x)Computes the value of 𝑒𝑥 ■ log(x)Computes ln(x) –the natural logarithm – ■ log10(x)Computes 𝑙𝑜𝑔10(𝑥) –the common logarithm – This function will not return complex results The e notation and exp(x) are not the same.
  • 5. Practice: ■ Define this vector x1 = [ -2 ,-1 ,0 , 1 , 2] A. Find the absolute value of each element of the vector. B. Find the square root of each element of the vector. ■ Use the ‘nthroot’ function to find the square root of -3 and 3. ■ Create a vector x2 from -9 to 12 with an increment of 3. A. Find ln(x) –the natural logarithm – B. Find Log10(x) –the common logarithm – ■ Use the sign function to determine which elements in x2 are positive.
  • 6. Rounding Functions ■ round(x) Rounds x to the nearest integer ■ fix(x)Rounds /truncates x to the nearest integer toward zero ■ floor(x) Rounds x to the nearest integer toward negative infinity ■ ceil(x)Rounds x to the nearest integer toward positive infinity
  • 7. Discrete Mathematics ■ factor(x)Finds the prime factors of x ■ gcd(x,y)Finds the greatest common divisor of x and y ■ lcm(x,y)Finds the least common multiple of x and y ■ rats(x)Represents x as a fraction ■ factorial(x)Finds the value of x!
  • 8. Discrete Mathematics ■ nchoosek(n,k)Finds the number of possible combinations of k items from a group of n items. ❖ Hint: • This instruction is used for combinations ( 𝐶(𝑛,𝑟) = 𝑛! 𝑟! 𝑛−𝑟 ! ) • For permutations (𝑃(𝑛,𝑟) = 𝑛! 𝑛−𝑟 ! ) you can use factorial(n) /factorial(n-r) ■ primes(x) Finds all the prime numbers less than x ■ isprime(x)Checks to see if x is a prime number.
  • 9. Practice: ■ Factor the number 322. ■ Find the greatest common divisor of 322 and 6 . ■ Is 322 a prime number? ■ Find the number of possible groups containing 3 people from a group of 29,when order does not matter.
  • 10. Trigonometric Functions ■ sin(x)Finds the sin of x when x is in Radians ■ asin(x)Finds the arcsin/ inverse of sine of x when x =[-1,1] • The function returns an angle in radians Ɵ=[− ᴨ 2 , ᴨ 2 ] ■ sinh(x)Finds the hyperbolic sine of x when x is in radians ■ asinh(x)Finds the inverse of the hyperbolic sin of x ■ sind(x)Finds the sin of when x is expressed in degrees ■ asind(x)Finds the inverse sin of x when x=[-1,1] • The function returns the angle in degrees You can use similar functions in cos(x), tan(x)
  • 11. Practice: ■ sin(2Ɵ) for Ɵ = 3ᴨ ■ Cos(Ɵ) for 0≤ 2ᴨ ; let Ɵ change in steps of 0.2ᴨ ■ 𝑠𝑖𝑛−1 (1) ■ 𝑐𝑜𝑠−1 𝑥 for -1≤ x ≤ 1; let Ɵ change in steps of 0.2 ■ Find the cosine of 45° ■ Find the angle whose sine is 0.5. Is your answer in degrees or radians? ■ Find the cosecant of 60. Calculate the following :
  • 12. Data Analysis Functions Maxima & Minima ■ max(x) Finds the largest value in each column. ❖ Hint: • Using the transpose extracts the maximum of each row, max(x’) ■ [a,b] = max(x)Assigns the maximum elements in each column to a and assigns their locations to b ■ max(x,y)Creates a x*y matrix, then compares between the two vectors You can use similar functions for minima. Use “min”
  • 13. Practice: • Create the following matrix: x= [4,90,85,75;2,55,65,75;3,78,82,79;1,84,92,93] ■ What is the maximum value in each column? ■ In which row does that maximum occur? ■ What is the maximum value in each row? ■ In which column does the maximum occur? ■ What is the maximum value in the entire table?
  • 14. Data Analysis Functions Averages ■ mean(x) Finds the mean value of each column of the matrix. ❖ Hint: • mean(mean(x))Finds the mean value of matrix x ■ median(x)Finds the median value in each column ■ mode(x)Finds the value that occurs most often in the array
  • 15. Practice: • Define the following matrix: x= [4,90,85,75;2,55,65,75;3,78,82,79;1,84,92,93] ■ What is the mean value in each column? ■ What is the median value in each column? ■ What is the mean value in each row? ■ What is the median value in each row? ■ What is returned when you call “ mode” function? ■ What is the mean for the entire matrix?
  • 16. Data Analysis Functions Sums & Products ■ sum(x)Sums the elements in each column ■ prod(x) Computes the product of each column ■ cumsum(x)Computes the cumulative sums of the elements ■ cumprod(x) Computes the cumulative products of the elements ❖ Hint: • sum(x(:,1)) Finds the sum of all the rows in the first column of matrix • sum(x(1,:)) Finds the sum of all the columns in the first row of matrix x
  • 17. Data Analysis Functions Sorting ■ sort(x)Sorts the matrix column wise in ascending order ■ sort(x,’descend’)Sorts the elements in each column in descending order ■ sortrows(x) Sorts the matrix row wise in ascending order on the bases of column 1( keeps the row intact) ■ sortrows(x,n)Sorts the rows based on the nth column, but maintains the rows unchanged. • If n is negative , the values are sorted in descending order. • If n is not specified , the default column is 1
  • 18. Data Analysis Functions Size ■ size(x)Determines the number of rows and columns in matrix x ■ [a,b]= size(x) Determines the number of rows and columns in matrix x then assigns rows to a and columns to b ■ length(x)Determines the number of the largest dimension in a matrix ■ numel(x)Determines the total number of elements in a matrix x
  • 19. Practice: • define the following matrix: x= [4,90,85,75;2,55,65,75;3,78,82,79;1,84,92,93] ■ Use the size function to determine the number of rows and columns in this matrix. ■ Use the sort function to sort each column in ascending order. ■ Use the sort function to sort each column in descending order. ■ Use the sortrows function to sort the matrix so that the first column is in ascending order. ■ Use the sortrows function to sort the matrix from the 4th exercise in descending order; based on the third column.
  • 20. Data Analysis Functions Statistical Functions ■ std(x) Computes the standard deviation of the values in a vector x. ■ var(x) Calculates the variance of x. • For 2d matrix use the operator (:) ■ std(x(:)) Computes the standard deviation of all the values in a matrix x ■ var(x(:)) Calculates the variance of all the values in a matrix x
  • 21. Practice: • Define the following matrix: x= [4,90,85,75;2,55,65,75;3,78,82,79;1,84,92,93] ■ Find the standard deviation for each column. ■ Find the variance for each column. ■ Calculate the square root of the variance you found for each column.
  • 22. Special Functions ■ i/j Means imaginary number ■ Inf Means infinity , which occurs during a calculational overflow or when a number is divided by zero ■ NaN Means not a number, occurs when a calculation is undefined ■ clock Returns current time ■ date Returns current date
  • 24. Complex Numbers ■ abs(x)Computes the absolute value of a complex ■ angle(x)Computes the angle from the horizontal in radius ■ complex(x,y) Generates a complex number with a real component x and an imaginary component y ■ conj(x)Generates the complex conjugate of a complex number ■ imag(A) Extracts the imaginary component of A ■ real(A) Extracts the real component of A imaginary real
  • 25. Practice: ■ Create the following complex numbers: A= 1+j B=2-3j C= 8+2j ■ Create a vector D of complex numbers whose real components are 2,4 and 6 and whose imaginary components are -3 , 8 and -16. ■ Find the magnitude (absolute value) of each of the vectors you created above. ■ Use the transpose operator to find the complex conjugate of vector D.
  • 27. Dot & Cross Product: ■ Dot Product : vector * vector = scalar ■ Cross Product: vector * vector= vector • Remember: A* B ≠ B* A Practice: • Define the following vectors: • Ԧ 𝐴 = [ 1, 2, 3, 4 ] , 𝐵 = [ 12, 20 ,15, 7 ] • Find the dot product of Ԧ 𝐴 .𝐵 A & B must have the same number of elements
  • 28. Matrix Inverse & Determinant ■ What is an inverse of a matrix? • It is the matrix when you multiply it by the original matrix you get an identity matrix • Identity matrix : is a matrix consists of ones down the main diagonal and zeros in all the other locations ■ In order for a matrix to have an inverse , it has to be a square matrix (m*m) ■ When a matrix has an inverse ,it is called an invertible matrix ■ When a matrix has no inverse ,it is called a singular matrix ■ To get the inverse of a matrix ,the determinant should not be 0 ■ If the determinant of a matrix is 0, the matrix has no inverse
  • 29. Special Matrices ■ diag(A) Extracts the diagonal of a two dimensional matrix A ■ fliplr(A) Flips a matrix into its mirror image ■ flipud(A) Flips a matrix vertically ■ magic matrix : is a matrix with unusual properties. • The sum of columns is the same as the sum of rows and also the sum of diagonals. • The sum of the diagonal line is equal to the sum of the diagonal line after flipping it. • Here is the proof • Try : diag(A, 1)
  • 30. Special Matrices ■ Identity matrix: • eye (m), eye(m,n) NOTE: • Do not name an identity matrix ‘i’ ,because ‘i’ will no longer represent −1 in any statements that follow ■ all  Determines if all the elements are non zero ■ any  Determines if any element is non zero ■ ones(m,n,z) Creates a 3 D matrix with m rows, n columns , z pages
  • 31. Special Matrices ■ expm(A) Returns matrix A’s exponential ■ sqrtm(A) Returns the square root of matrix A ■ eig(A) Returns eigenvalues and eigenvectors of matrix A
  • 33. Basic Plotting ■ Try these commands: • x= [ 0:2:18]; • y = exp(x)-3; • plot(x,y) • title(‘ lab experiment ‘) • xlabel(‘ x axis ’) • ylabel(‘ y axis ’) • grid on create a graph first , then add title and axis labels.
  • 36. Basic Plotting ■ plot Creates an x-y plot ■ title(‘TEXT’) Adds title to a plot ■ xlabel(‘TEXT’)  Adds a label to the x axis ■ ylabel (‘TEXT’) Adds a label to the y axis ■ grid Adds a grid to the graph ■ pause  Pauses the execution of the program ,allowing the user to view the graph ■ figure Determines which figure will be used for the current plot ■ hold Freezes the current plot , so that an additional plot can be overlaid.
  • 37. Multiple plots ■ To plot two functions on the same window: ■ To use new window to plot a new graph : • Use figure(#)
  • 38. Plotting a Matrix ■ Consider the following code: ■ If the plot contains a matrix , MATLAB does a separate line for each column, and the x-axis becomes the row vector
  • 39. Subplots ■ The ‘subplot’ command allows you to subdivide the graphing window into a grid of m rows and n columns. • subplot(m,n,p) No. of rows No. of columns location
  • 40. Practice: ■ Subdivide a figure window into two rows and one column. ■ In the top window ,plot y =tan(x) for -1.5 ≤ x ≤ 1.5 (use an increment of 1). ■ Add a title and axis labels to your graph. ■ In the bottom window ,plot y =sinh(x) for the same range. ■ Add a title and labels to your graph. ■ Try this exercise again ,but divide the figure window vertically instead of horizontally.
  • 41. Other Functions ■ To generate a square wave : • Use square ( 𝜔 *t , rho) ■ To generate a triangular wave : • Use sawtooth(𝜔 *t , rho) ■ Polar Plots: • Use polar(theta, r)
  • 42. Practice: *Hint : Use the command: polar(theta, r) ■ Declare theta from 0 to 2pi ,in steps of 0.01*pi. 1. Define an array r= 5*cos(4*theta), then plot it in polar form. 2. Define an array r= 4*cos(6*theta), then plot it in polar form. 3. Define an array r= 5 –5*sin(theta), then plot it in polar form. 4. Define an array r= sqrt(5^2*cos(2*theta)), then plot it in polar form. ■ Declare theta = pi/2:4/5*pi:4.5pi • Define an array r which is a six-member array of ones ,then plot it in a polar form.
  • 43. Editing the Graph ■ Changing the shape of line , mark and style. • They come in this order: ( ‘ line, point type , color’) ■ Axis scaling: • axis([ x,min x,max y,min y,max]) Max of y-axis Max of x-axis Min of x-axis Min of y-axis Dotted line Circled points Black color
  • 44. Line, Mark , and Color Options : Line Type Indicator Point Type Indicator Color Indicator solid - point . blue b dotted : circle o green g dash-dot -. x-mark x red r dashed - - plus + cyan c star * magenta m square s yellow y diamond d black k triangle down v white w triangle up ^ triangle left < triangle right > pentagram p hexagram h
  • 45. Editing the Graph ■ axis(v) v is the input to axis command , it must be 4 element vector ■ axis equal  Forces the scaling on the x and y axis to be the same ■ legend(‘ string1 , ‘string2’) Allows you to add legends to your graph ■ text(x_coordinate , y_coordinate , ‘TEXT’) Adds a text at point (x,y) ■ gtext(‘ string’) Similar to the ‘text’ command , but the location is determined by the user by clicking in the figure window manually.
  • 46. Pie and Bar Graphs ■ Try the following:
  • 47. Pie and Bar Graphs ■ bar(x) When x is a vector , ‘bar’ generates a vertical bar graph and when x is 2d matrix ,bar groups the data by row ■ barh(x) Similar to bar , but generates horizontal bars ■ bar3(x) Generates a 3d bar chart ■ barh3(x) Generates a 3d horizontal bar chart ■ pie(x) Generates a pie chart. Each element of the matrix is represented as a slice of a pie ■ pie3(x) Generates a 3d pie chart.