What is Matlab
Getting Started
MATLAB Desktop:
Launch Pad: displays all the tools and
applications associated with MATLAB;
Workspace: consists of the variables you
create during a MATLAB session;
Command History: double click them to
evaluate them;
Current Directory browser: shows you where
you are.
Editor/Debugger: pops up when you create an
M-files (click on New button to launch it.)
6
-2
-1
x = -2.9:0.2:2.9;
bar(x,exp(-x.*x));
8
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0.5
1.5
2.5
3.5
4.5
Line plot:
x=0:0.05:5;y=sin(x.^2);plot(x,y);
9
0.5
1.5
2.5
3.5
Stem plot:
x = 0:0.1:4;, y = sin(x.^2).*exp(-x);
stem(x,y)
10
10
5
0
-5
-10
30
20
10
0
10
15
20
25
11
10
5
0
-5
-10
30
20
10
0
10
15
20
25
Surface plot:
z=peaks(25);, surf(z);, colormap(jet);
12
20
15
10
10
15
20
25
13
14
Matrix
Matlab works with essentially only one kind of object a rectangular
numerical matrix with possible complex entries.
Matrices can be
Entered manually;
Matrix
Examples
x = [ 3.5, 33.22, 24.5 ] ;
x1 = [ 2
5
3
-1];
The matrix name can be any group of letters and numbers up to 63, but
always beginning with a letter. howaboutthisname how_about_this_name
Matlab is "case sensitive", that is, it treats the name 'C' and 'c' as two
different variables.
Similarly, 'MID' and 'Mid' are treated as two different variables.
16
Matrix Reference
1
2
3
4
5
6
7
8
9
10
11
12
A(1,1)
A(2,1)
A(3,1)
A(4,1)
A(1,2)
A(2,2)
A(3,2)
A(4,2)
A(1,3)
A(2,3)
A(3,3)
A(4,3)
1st element
2nd element
3rd
4th
5th
6th
7th
8th
9th
10th
11th
12th
linear index
Matrix Reference
A(b)
Syntax in Matlab
Colon operator: The colon operator ' : ' is understood by Matlab to perform special
and useful operations.
For example, if two integer numbers are separated by a colon, Matlab will generate
all of the integers between these two integers.
a = 1:8
generates the row vector, a = [ 1 2 3 4 5 6 7 8 ].
If three numbers, integer or non-integer, are separated by two colons, the middle
number is interpreted to be a step and the first and third are interpreted to be limits:
b = 0 :0 .2 : 1.0
generates the row vector b = [ 0.0 .2 .4 .6 .8 1.0 ]
19
Syntax in Matlab
The colon operator can be used to create a vector from a matrix.
Thus if
x=[2 6 8
0 1 7
-2 5 -6 ]
The command y = x(:,1) creates the column vector
y=[ 2
0
-2 ]
The command z = x(1,:) creates the row vector
z=[2 6 8]
What is w = x(2:end,:)?
20
Syntax in Matlab
The colon operator is useful in extracting smaller matrices from larger matrices.
If the 4 x 3 matrix c is defined by
c = [ -1 0
1 1
1 -1
0 0
Then
0
0
0
2]
0
0
0
2]
21
Matrix Operations
+ addition
- subtraction
* multiplication
^ power
transpose for a real matrix and complex conjugate transpose
for a complex matrix (transpose for a complex matrix is .)
\ left division, / division
x = A \ b is the solution of A * x = b
x = b / A is the solution of x * A = b
Matrix math
Dimensions must agree
Scalar math
Same as usual
Scalar / matrix math
Scalar + matrix = [scalar + matrix(i, j)]
Scalar * matrix = [scalar * matrix(i, j)]
22
Matrix Operations
To make the * , ^, \ and / entry-wise, we precede the operators
by .
a .* b multiplies each element of a by the respective element
of b
a ./ b divides each element of a by the respective element of b
a .\ b divides each element of b by the respective element of a
a .^ b raise each element of a by the respective b element
Example
x = [1 2 3]; y = [4 5 6];
x * y = 32
x .* y = [4 10 18]
23
Matrix Operations
diag: Diagonal matrices or diagonals of a
matrix.
diag(A) for matrix A is the diagonals of A.
A= 0.1531 0.4929 0.1478
0.0495 0.6107 0.4326
0.9340 0.1224 0.7917
Then diag(A)= [ 0.1531; 0.6107; 0.7917]
blockdiag: Construct a block diagonal matrix
blockdiag(A,B,C)= A . . . 0
...
...
...
C
24
26
27
matrix,
a row vector whose elements are the standard deviations of each column of
the matrix
sort(x) sorts the values in the vector x or the columns of a matrix and places them
in ascending order.
hist(x) plots a histogram of the elements of vector, x. The bins are scaled based on
the max and min values
hist(x,n) plots a histogram with 'n' bins scaled between the max and min values
of the elements
hist((x(:,2)) plots a histogram of the elements of the 2nd column from the matrix x
corr(x) returns a pairwise correlation coefficient between the columns in X
28
Useful Constants
NaN the arithmetic representation for Not-a-Number, a NaN is
obtained as a result of mathematically undefined operations like
0.0/0.0
Inf the arithmetic representation for positive infinity, a infinity
is also produced by operations like dividing by zero, e.g.
1.0/0.0, or from overflow, e.g. exp(1000).
>> exp(1000)
ans = Inf
eps
machine epsilon
ans
pi
3.14159.
i and j
demo
who
whos
clear
31