You are on page 1of 29

MATrix LABoratory

Getting Familiar

MATLAB Training
Workshop-2016 at
Karachi Institute of
Power Engineering

Fahad Wallam
Presentation Outline

 Introduction to MATLAB
 MATLAB Environment
 Matrix IOs
 Variables, Numbers and Operators
 Matrix Operations
 Plotting with MATLAB
 MATLAB Structures
 Hands-on Activity

MATLAB Workshop – KINPOE 11.07.2019 Fahad Wallam 2


What is Matlab?

 A high-level language with interactive


MATLAB
environment for
 algorithm development
 data visualization High Level
languages such as
 data analysis and C, Fortran, Pascal etc.
 numeric computation
 Originally designed for solving linear
algebra type problems using matrices Assembly
 Performs faster computing than traditional
programming languages; especially those
involving matrix and vector manipulation

MATLAB Workshop – KINPOE 11.07.2019 Fahad Wallam 3


Application Areas

 Signal processing
 Image processing
 Communications
 Control design, test and measurement
 Financial modeling and analysis
 Computational biology
 Many others

MATLAB Workshop – KINPOE 11.07.2019 Fahad Wallam 4


Features

 High-level language
for technical computing
 Development environment
for managing code, files, and data
 Interactive tools
for iterative exploration, design, and problem solving
 Mathematical functions
for linear algebra, statistics, Fourier analysis, and
optimization, etc.

MATLAB Workshop – KINPOE 11.07.2019 Fahad Wallam 5


Features

 Graphics functions
for visualizing data in 2-D and 3-D
 MATLAB API
for integrating external applications and languages
such as C, Fortran, MS Excel etc. with MATLAB
 GUI tools
for building customize graphical user interfaces

*API Application Program Interface


GUI Graphic User Interface

MATLAB Workshop – KINPOE 11.07.2019 Fahad Wallam 6


Basic MATLAB System

Series of
MATLAB MATLAB
commands

Command
M-files Mat-files
line

functions Data storage


Command or loading
execution like DOS
command window
Input output
capability

MATLAB Workshop – KINPOE 11.07.2019 Fahad Wallam 7


MATLAB IDE
 Command window
 Type commands
 Current directory
Command
 View folders and m-files window
Current
 Workspace directory
Workspace
 View program variables
 Double click on a variable to see it in the array editor
Command
 Command history history

 View past commands


 Save whole session
*IDE Integrated Development Environment

MATLAB Workshop – KINPOE 11.07.2019 Fahad Wallam 8


MATrix LABoratory

 Every entity in MATLAB – Matrix


 Matrix – Rectangular array of numbers or
an array of vectors
 Vector – Matrix having only one row or
column
 String – Vector of characters
 1-by-1 Matrix – Scalar

An image is also a 3D matrix


MATLAB Workshop – KINPOE 11.07.2019 Fahad Wallam 9
Matrix Operation (Review)

MATLAB Workshop – KINPOE 11.07.2019 Fahad Wallam 10


Matrix Input/Output

 Entering a matrix – Several ways


 Entering explicit list of elements
 Generating through built-in functions
 User defined functions to create
 Loading from external data files

 Viewing a matrix
 Full view
Entering the name displays the complete matrix – A
 Selected portion view
From i th to j th column of p th to q th rows of a matrix, a colon
“:” is used – A(p:q,i:j)
* Where A is the matrix name

MATLAB Workshop – KINPOE 11.07.2019 Fahad Wallam 11


Creating Matrices

 Matrix
» A = [1, 2; 3 4];
A =
1 2
3 4
» B = [ 1 3 5 7;
2,4,6,8
9 8,7 6; 5, 4, 3, 2]
 Vector
» x = [1 2 3 4] xB ==
11 23 35 47
 Transpose y = 2 4 6 8
» y = x’ 19 8 7 6
25 4 3 2
3
4
MATLAB Workshop – KINPOE 11.07.2019 Fahad Wallam 12
Extracting Sub Matrix

 Matrix name is a variable that can be modified


 Element in i th row and j th column of matrix A is
denoted by A(i,j)
 Indexing always starts from 1
 To address from i th to j th column of p th to q th
rows, syntax A(p:q,i:j) is used; for example:
A = Ans =
A=[1 3 5
1 3 5 77;2 4 6 8;9 8 7 6;5 4 3 2]
2 4 6 8 2 4 6
9 8 7 6 A(2:4,1:3) 9 8 7
What 4 3 2 be the result of A(:,1:2) 5& 4
5 would 3
A(3:4,:)?
MATLAB Workshop – KINPOE 11.07.2019 Fahad Wallam 13
Variables and Numbers

 Variables
 Variable names are case sensitive; must start with a
letter
 Default length of variable name is 63; can be changed
 Type, declarations or dimension are not required
 Numbers
 Conventional decimal notation with optional decimal
point, and leading plus or minus sign
 Scientific notation uses the letter e to specify a
power-of-ten scale factor
 Imaginary numbers use either i or j as a suffix
MATLAB Workshop – KINPOE 11.07.2019 Fahad Wallam 14
Some Special Variables

 ans Default variable name for results


 pi Value of 
 inf Infinity
 NaN Not a number e.g. 0/0
 i and j i = j = square root of -1

MATLAB Workshop – KINPOE 11.07.2019 Fahad Wallam 15


Examples of Numbers

 3 , -99
 0.0001 , 9.6397238
 6.02252e23 , 1.60210e-20
 1i , -3.141 + 59j
 3e5i

Numbers are stored internally using long


format specified by the IEEE® floating-point
standard with a precision of roughly 16
significant decimal digits and a range of
roughly 10-308 to 10+308
MATLAB Workshop – KINPOE 11.07.2019 Fahad Wallam 16
Arithmetic Operators

 + Addition
 - Subtraction
 * Multiplication
 / Division A/B  AB-1
 \ Left division A\B A-1B
 ^ Power A^B  AB
 ’ Complex conjugate transpose
 ( ) Specify evaluation order
 = Assignment operator NOTE: 56/8 = 8\56
MATLAB Workshop – KINPOE 11.07.2019 Fahad Wallam 17
Relational and Logical Operator
 Relational operators
 Less than <
 Less than or equal <=
 Greater than >
 Greater than or equal >=
 Equal to ==
 Not equal to ~=
 Logical operators
 not ~ highest precedence
 and & equal precedence with or
 or | equal precedence with and
MATLAB Workshop – KINPOE 11.07.2019 Fahad Wallam 18
Element-Wise Operation

 Operation between elements of similar indices in


two matrices of same dimensions
 Does not effect addition or subtraction as these
operations are originally element-wise performed
 A period ‘.’ before an operation makes it element
wise operation
A.*B = A(1,1)*B(1,1) A(1,2)*B(1,2) …….. ………….. A(1,n)*B(1,n)
A(2,1)*B(2,1) A(2,2)*B(2,2) …….. ………….. A(2,n)*B(2,n)
…….. ……. …….. …….. ……..
A(m,1)*B(m,1) A(m,2)*B(m,2) …….. ……….. A(m,n)*B(m,n)

MATLAB Workshop – KINPOE 11.07.2019 Fahad Wallam 19


Concatenation

 Joining small matrices to make bigger ones

 Wow!!! You are already done with this…. How?


Made your first matrix by concatenating individual elements

 Square brackets “[]” – Concatenation operator

 A new row or new column can be concatenated

 Strictly requires dimension match


Try this:
If A=[1 2 3;4 5 6] B=[5 7 9;4 6 8], find C=[A B] & D=[A;B]
MATLAB Workshop – KINPOE 11.07.2019 Fahad Wallam 20
Deleting Rows and Columns

Row(s) or column(s) can be deleted from a matrix


just by using a pair of square brackets
 To delete the i th row of matrix X
X(i,:) = []
 To delete the i th column of matrix X
X(:,i) = []
 Deleting single element from a matrix (like
below) results in an error
X(1,2) = []
MATLAB Workshop – KINPOE 11.07.2019 Fahad Wallam 21
Deleting Elements & Suppressing Output

 Unlike matrix, element(s) can be deleted from a


vector using blank vector; for example:
 A(3)=[] eliminates 3rd element from vector A
 A(2:5)=[] eliminates 2nd to 5th elements from A
 Suppressing Output
 Entering anything in Matlab displays a resulting matrix
 Sometimes the result is not needed to be displayed
 Resulting matrix may require large space in command
window and extensive time for displaying on screen
 In such cases, the output can be suppressed by using
a semi-colon “;” at the end of command
MATLAB Workshop – KINPOE 11.07.2019 Fahad Wallam 22
Some Useful Stuff

 Entering long statements


 Use an ellipsis (three periods) “...” followed by Enter
key to continue the statement on the next line
s = 1-1/2+1/3-1/4+1/5-1/6+1/7+1/8 +1/9...
-1/10+1/11-1/12;
 Getting help
 Type help followed by the keyword to get the help
» help format
 Some frequently used commands
 who Lists current variables
 clc Clears the display
 clear Clears variables and functions from memory
MATLAB Workshop – KINPOE 11.07.2019 Fahad Wallam 23
Generating Matrix Through
Built-in Functions

 zeros All zeros


» zeros(m,n) or » zeros(m)
 ones All ones
» ones(m,n) or » ones(m)
 rand Uniformly distributed random elements
» rand(m,n) or » rand(m)
 randn Normally distributed random elements
» randn(m,n) or » randn(m)
*m number of rows
n number of column

MATLAB Workshop – KINPOE 11.07.2019 Fahad Wallam 24


Scalar – Matrix Operations

 Addition  Subtraction
Add scalar number to Subtract scalar number
each element of the from each element of the
matrix matrix
» a=3; » a=3;
» b=[1, 2, 3;4, 5, 6]; » b=[1, 2, 3;4, 5, 6];
» c= b+a » c= b-a
c= c=
4 5 6 -2 -1 0
7 8 9 1 2 3

MATLAB Workshop – KINPOE 11.07.2019 Fahad Wallam 25


Scalar – Matrix Operations

 Multiplication  Division
Multiply each element of Multiply each element of
matrix by the scalar matrix by the scalar
number number
» a=3; » a=3;
» b=[1, 2, 3;4, 5, 6]; » b=[1, 2, 3;4, 5, 6];
» c= a*b » c= b/a
c= c=
3 6 9 0.3333 0.6667 1.0000
12 15 18 1.3333 1.6667 2.0000

MATLAB Workshop – KINPOE 11.07.2019 Fahad Wallam 26


Recap

 Became accustomed with MATLAB


 Creating a matrix and extracting sub matrix
 Joining small matrices to form bigger ones
 Deleting rows, columns or elements
 Commonly used commands and functions
 Scalar-matrix operations

MATLAB Workshop – KINPOE 11.07.2019 Fahad Wallam 27


Thank you !

MATLAB Workshop – KINPOE 11.07.2019 Fahad Wallam 28


Enhance Your Learning
 Given x = [3 1 5 7 9 2 6], explore the following commands:
x(3), x(1:7) , x(1:end), x(1:end-1), x([1 6 2 1 1]), sum(x)
 Given A = [2 7 9 7 ; 3 1 5 6 ; 8 1 2 5], explore the results of
following commands:
A', A(:,[1 4]), A([2 3],[3 1]), A(:), flipud(A), fliplr(A), [A A(end,:)],
A(1:3,:), [A ; A(1:2,:)], sum(A), sum(A'), sum(A,2)
 Refer to slide number 20, try C and D if B=[5 6 7]
 Given the array A above, investigate the command that will
 assign the first row of A to a vector called x
 assign the last 2 rows of A to an array called y
 delete the element A(3,2)
 Explore the syntax of following MATLAB commands by using help
whos, home, reshape, rem, magic, length, input, error
MATLAB Workshop – KINPOE 11.07.2019 Fahad Wallam 29

You might also like