Matlab 1
Matlab 1
School of Engineering
Department of Electrical and Computer Engineering
Joe Khalifeh
Introduction
MATLAB=Matrix Laboratory
MATLAB is a high-performance language for technical
computing.
It integrates computation, visualization, and programming
in an easy-to-use environment.
2
MATLAB System
3
MATLAB Toolboxes
4
MATLAB Windows
Command Window
Current Directory
Workspace
Command History
5
Basic Notations
Semicolon(;): If a semicolon is typed after a command, then the
command is executed without displaying the output.
Comments(%): Similarly to high level programming languages,
comments in MATLAB codes are written after typing percent
sign %
clear: It clears all variables in workspace
clear A B: Clears variables A and B from workspace
clc: Clears the command window and homes the cursor. It
doesn’t affect workspace variables
close: Closes the current figure window
help plot: Gives information about the use and the arguments of
a function. In this case, it gives information about the function
“plot”
exit: Exit MATLAB
6
Arithmetic Operators
Symbol Operation
+ Addition
- Subtraction
* Multiplication
/ Division
\ Left division
^ Power
' Complex conjugate transpose
() Specify evaluation order
7
Arithmetic Operations
MATLAB as a calculator:
Simplest way to use MATLAB
Type command (mathematical expression)
Press Enter Key
Command executed and then is displayed
o ans= (result)
Example
cos(pi/2)
ans = 0
8
Display Format
The number format in command window can be
modified using the command format
The default format in the command window is the
short representation of numbers.
9
Display Format
11
MATLAB Variables
The MATLAB language works with only a single object
type: the MATLAB array.
All MATLAB variables, including scalars, vectors,
matrices, strings, cell arrays, structures, and objects
are stored as MATLAB arrays.
Variables are shown in Workspace.
Variables can have different data types such as:
Complex Double-Precision Matrices
Numeric Matrices
Logical Matrices
MATLAB Strings
Empty Arrays
12
MATLAB Variables
Variable
Name made of a combination of letters and/or digits:
Memory location
Scalar variables are assigned a numerical value:
Stored in memory location
Can be used in any MATLAB statement or command
13
MATLAB Variables
Rules about variable names:
Up to 63 characters in MATLAB 7 (31 in MATLAB 6.x).
Can contain letters, digits and underscore.
Must begin with a letter.
MATLAB is case sensitive.
Avoid using names of built-in functions or predefined variables.
Predefined variables
pi = the number π
Inf =Infinity
realmax=Largest positive floating point number
realmin=Smallest positive floating point number
i = sqrt(-1)
j=I
NaN= (Not a Number) used by MATLAB when it cannot define a valid numerical
value, such as 0/0.
Eps = Spacing of floating point numbers = 2-52
14
MATLAB Variables
Command Description
Clear variables and functions from
clear memory.
15
Creating Arrays in MATLAB
Array:
Fundamental form used to store and manipulate data.
Arranged in rows and/or columns.
Include data of different types.
Arrays are n-dimensional:
One-Dimensional (Vector)
Two-Dimensional (Matrix)
N-Dimensional
16
Arrays
Array constructor [ ]
An array of elements (Vector or Matrix) is created using
brackets [ ]
Example:
V=[1 2 3 5] creates a horizontal vector
Similarly, V=[1,2,3,5]
17
Creating Vectors
When vector elements are specified element by element,
a vector can be defined as follows:
Row vector:
V=[1 2 3 5]
V=1 2 3 5
Column Vector:
Elements in a column vector are separated using semicolon(;)
U=[5;2;1]
U= 5
2
1
18
Creating Vectors
Vectors with constant spacing:
V=start: space :end
start= first element, end=last element
space= spacing between two consecutive elements
V=1:3:13
V=1 4 7 10 13
19
Creating Vectors
V=linspace(start,end,# of elements)
V=linspace(1,5,3)
V =1 3 5
20
Creating Matrices
Matrices are two-dimensional arrays.
An m-by-n matrix has m rows and n columns
All rows must have the same number of elements.
In square matrices, m=n.
Example:
A=[1 5 7;8 2 6;4 -2 9]
A=1 5 7
8 2 6
4 -2 9
21
Creating Matrices
Variables or functions with adequate output size can be
used to define matrix elements.
x=0;
y=pi/6;
z=pi/2;
A=[x,y,z]
A=0 0.5236 1.5708
B=[A;sin(A)]
B=0 0.5236 1.5708
0 0.5 1
22
Useful matrices
zeros(M,N)
Creates an M-by-N matrix of zeros.
ones(M,N)
Creates an M-by-N matrix of ones.
eye(N)
Creates the N-by-N identity matrix.
23
3-Dimensional Arrays
Example:
A=[1 2 5;7 8 6];
B=[8 2 6;7 3 1];
C(:,:,1)=A
C=1 2 5
7 8 6
C(:,:,2)=B
C(:,:,1) =1 2 5 C(:,:,2) = 8 2 6
7 8 6 7 3 1
24
The Transpose Operation
In vectors: Switches row (column) to column (row)
In matrices: Switches columns (rows) to rows (columns)
Applied by typing ‘ next to a variable.
Transpose is not defined for N-Dimensional arrays where
N>2
Example:
A = [1 2 5; 7 8 6]
A=1 2 5
7 8 6
>> A'
ans = 1 7
2 8
5 6
25
Array Addressing
For example:
V=[5 4 8 3 7];
V(1) a=V(5)
ans =5 a=7
26
Array Addressing
The element “-9” is in the 2nd row and 3rd column can be
addressed by:
A(2,3)
ans =-9
27
Array Addressing
To address sub-matrices in a matrix, we use the colon (:)
notation. Consider the following matrix:
A(2:3,1:2)
ans = 3 2
1 4
28
Array Addressing
>> A=[5 6 9;3 2 7;1 4 8]
A= 5 6 9
3 2 7
elements from (1st and
1 4 8 3rd row) and (1st and
2nd column)
A([1 3],[1 2])
ans = 5 6
1 4
29
Array Addressing
A= 5 6 9
3 2 7
1 4 8
A(:,2)
ans = 6
2
4
30
Modifying array elements
Modifying array elements can be done by assigning
new elements to sub-parts of the array.
A= 5 6 9
3 2 7 5 8
1 4 8 6 3
A(2:3,1:2)=[5 8;6 3]
A= 5 6 9
5 8 7
6 3 8
31
Adding elements to arrays
32
Adding elements to arrays
Example: A = 5 6 9
3 2 7
1 4 8
A(:,5)=[3;7;2]
A= 5 6 9 0 3
3 2 7 0 7
1 4 8 0 2
Note that the 4th column is automatically created and set to 0, and in
this horizontal appending, the number of rows of the original and
added matrices are equal.
33
Adding elements to arrays
Adding a single element to an array is always allowed (without
constraints on the size of the matrix).
A= 5 6 9
3 2 7
1 4 8
A(5,4)=2
A= 5 6 9 0
3 2 7 0
1 4 8 0
0 0 0 0
0 0 0 2
34
Adding elements to arrays
Another method of appending elements of two arrays is by assigning a
new array whose elements are arrays and not scalars.
A=[1 2;5 6]
A=1 2
5 6
B=[7;8]
B=7
8
C=[A B]
C= 1 2 7
5 6 8
35
Deleting elements from arrays
A=5 6 9
3 2 7
1 4 8
A(:,2)=[ ]
A=5 9
3 7
1 8
36
Array Functions
Function Description Example
Returns the M-by-N matrix X=[1 2;3 4];
reshape(X,M,N) whose elements are taken
column wise from X.
Y=reshape(X,1,4)
Y=1 3 2 4
v=[1 2 3];
Returns a matrix and puts the A=diag(v)
diag(v) elements of v in the main
diagonal
A= 1 0 0
0 2 0
0 0 3
Returns the M-by-N matrix X=[1 2;3 4];
reshape(X,M,N) whose elements are taken
column wise from X.
Y=reshape(X,1,4)
Y=1 3 2 4
for matrix X, returns the X=[1 2 3;7 5 9];
number of rows and columns
[M,N]=size(X) in X as separate output
[M N]=size(X)
variables. M=2 & N=3
37
Simple plot with MATLAB
Example:
Plot the function x=2exp(-2t) over the range [0;2]
t=0:0.1:2;
x=2*exp(-2*t);
plot(t,x)
grid
38
Simple plot with MATLAB
39