MATLAB - Lecture 1 - Overview
MATLAB - Lecture 1 - Overview
MATLAB - Lecture 1 - Overview
Krishna K. Mohbey
MANIT Bhopal
MATLAB introduction
MATLAB is a program for doing numerical
computation.
It was originally designed for solving linear
algebra type problems using matrices.
It’s name is derived from MATrix LABoratory.
MATLAB has since been expanded and now has
built-in functions for solving problems requiring
data analysis, signal processing, optimization, and
several other types of scientific computations.
It also contains functions for 2-D and 3-D graphics
and animation.
MATLAB
Matlab is basically a high level language which has
many specialized toolboxes for making things easier
for us
How high? Matlab
High Level
Languages such as
C, Pascal etc.
Assembly
What are we interested in?
Matlab is too broad for our purposes in this
course.
The features we are going to require is
Series of Matlab
Matlab
commands
Command
m-files Line
mat-files
functions
Input
Output Command Data
capability execution like DOS storage/
command window loading
Matlab Screen
Command Window
type commands
Current Directory
View folders and m-files
Workspace
View program variables
Double click on a variable
to see it in the Array Editor
Command History
view past commands
save a whole session
using diary
MATLAB Help
• MATLAB Help is an extremely
powerful assistance to learning
MATLAB
C/C++ Excel /
Java COM
Perl
File I/O
Deploying with MATLAB
COM Excel
MATLAB
The MATLAB environment is command oriented
somewhat like UNIX.
A prompt appears on the screen and a MATLAB
statement can be entered.
When the <ENTER> key is pressed, the statement is
executed, and another prompt appears.
If a statement is terminated with a semicolon ( ; ), no
results will be displayed.
Otherwise results will appear before the next prompt.
The MATLAB User Interface
More about the Workspace
• who, whos – current variables in the
workspace
• save – save workspace variables to *.mat file
• load – load variables from *.mat file
• clear – clear workspace variables
- CODE
MATLAB
» a=5;
» b=a/2
b=
2.5000
»
Variables
No need for types. i.e.,
int a;
double b;
float c;
All variables are created with double precision unless
specified and they are matrices.
Example:
>>x=5;
>>x1=2;
After these statements, the variables are 1x1 matrices with
double precision
MATLAB Variable Names
>> e=1/3
e=
0.3333 %default
>> format long
>> e
e=
0.333333333333333 %long decimal
>> format short e
>> e
e=
3.3333e-001 %long exponential
To clear a variable
» who
D ans rho
NRe mu v
» clear D
» who
»
Complex Numbers
» c1 = 2+3i
c1 =
2.0000 + 3.0000i
»
Complex Numbers
Some functions deal with complex number
>> c=1-2i
c = 1.0000 - 2.0000i
>> abs(c)
ans = 2.2361
>> real(c)
ans = 1
>> imag(c)
ans = -2
>> angle(c)
ans = -1.1071
Mathematical Functions
» x=sqrt(2)/2
x=
0.7071
» y=sin(x)
y=
0.6496
»
Built-in Functions
- (unary) + (unary)
Addition + a+b
Subtraction - a-b
Assignment = a=b (assign b to a)
Other MATLAB symbols
>> prompt
... continue statement on next line
, separate statements and data
% start comment which ends at end of line
; (1) suppress output
(2) used as a row separator in a matrix
: specify range
MATLAB Relational Operators
» x=23
x=
23
MATLAB Matrices
A matrix with only one row is called a row vector. A row
vector can be created in MATLAB as follows (note the
commas):
rowvec =
12 14 63
MATLAB Matrices
colvec =
13
45
-2
MATLAB Matrices
A matrix can be created in MATLAB as follows (note the
commas AND semicolons):
» a = [1 , 2 , 3 ; 4 , 5 ,6 ; 7 , 8 , 9] Or
» a = [1 2 3 ; 4 5 6 ; 7 8 9] Or
>> a=[1 2 3
456
7 8 9]
a=
1 2 3
4 5 6
7 8 9
Extracting a Sub-Matrix
sub_matrix = matrix ( r1 : r2 , c1 : c2 ) ;
» matrix=[1,2,3;4,5,6;7,8,9] » coltwo=matrix( : , 2)
matrix = coltwo =
1 2 3
4 5 6 2
7 8 9 5
8
MATLAB Matrices
matrix = » rowvec=matrix(2 : 2 , 1 : 3)
1 2 3 rowvec =
4 5 6
7 8 9 4 5 6
Special Matrices
1 0 0 0 0
eye(3) 0 1 0 zeros(3,2) 0 0
0 0 1 0 0
1 1 1
1 1 1 1
ones (3) 1 1 1 ones(2,4)
1 1 1 1 1 1 1
Special Matrices functions
>> a=magic(4) %magic matrix
a=
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
b=
x = [1 2], y = [4 5]
A = [ x y]
1 2 4 5
B = [x ; y]
12
45
Matrices Operations
Given A and B:
» a=3;
» b=[1, 2, 3;4, 5, 6]
b=
1 2 3
4 5 6
» c= b+a % Add a to each element of b
c=
4 5 6
7 8 9
Scalar - Matrix Subtraction
» a=3;
» b=[1, 2, 3;4, 5, 6]
b=
1 2 3
4 5 6
»c=b-a %Subtract a from each element of b
c=
-2 -1 0
1 2 3
Scalar - Matrix Multiplication
» a=3;
» b=[1, 2, 3; 4, 5, 6]
b=
1 2 3
4 5 6
»c=a*b % Multiply each element of b by a
c=
3 6 9
12 15 18
Scalar - Matrix Division
» a=3;
» b=[1, 2, 3; 4, 5, 6]
b=
1 2 3
4 5 6
»c=b/a % Divide each element of b by a
c=
0.3333 0.6667 1.0000
1.3333 1.6667 2.0000
The use of “.” – “Element” Operation
Given A:
M = mean(A), M = median(A)
M = mean(A,dim), M = median(A,dim)
Example:
A = [ 0 2 5 7 20] B = [1 2 3
336
468
4 7 7];
mean(A) = 6.8
mean(B) = 3.0000 4.5000 6.0000 (column-wise mean)
mean(B,2) = 2.0000 4.0000 6.0000 6.0000 (row-wise mean)
Mean and Median
Examples:
A = [ 0 2 5 7 20] B = [1 2 3
336
468
4 7 7];
Mean:
mean(A) = 6.8
mean(B) = 3.0 4.5 6.0 (column-wise mean)
mean(B,2) = 2.0 4.0 6.0 6.0 (row-wise mean)
Median:
median(A) = 5
median(B) = 3.5 4.5 6.5 (column-wise median)
median(B,2) = 2.0
3.0
6.0
7.0 (row-wise median)
Standard Deviation and Variance
Standard deviation is calculated using the std() function
std(X) : Calcuate the standard deviation of vector x
If x is a matrix, std() will return the standard deviation of each column
Variance (defined as the square of the standard deviation) is calculated using
the var() function
var(X) : Calcuate the variance of vector x
If x is a matrix, var() will return the standard deviation of each column
X = [1 5 9;7 15 22]
s = std(X)
s = 4.2426 7.0711 9.1924
Histograms
• Histograms are useful for showing the pattern of the
whole data set
• Allows the shape of the distribution to be easily
visualized
Histograms
Matlab hist(y,m) command will generate a frequency histogram
of vector y distributed among m bins
Also can use hist(y,x) where x is a vector defining the bin
centers
Example:
>>b=sin(2*pi*t)
>>hist(b,10); >>hist(b,[-1 -0.75 0 0.25 0.5 0.75
40
45
40 35
35 30
30
25
25
20
20
15
15
10
10
5 5
0 0
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 -1.5 -1 -0.5 0 0.5 1 1.5
Histograms
The histc function is a bit more powerful and allows bin edges to
be defined
x = statistical distribution
Example:
>> test = round(rand(100,1)*10)
>> histc(test,[1:1:10])
>> Bar(test) 14
12
10
0
1 2 3 4 5 6 7 8 9 10
Some Useful MATLAB commands
• CODE
Some Examples
>> x=20
x = 20
>> y=30
y = 30
>> if x>y
'greater x'
else
'greater y'
end
ans =greater y
>>
Some Examples
Example
x = [-3 -2 -1 0 1 2 3];
y1 = (x.^2) -1;
% Plot y1 on the top
subplot(2,1,1);
plot(x, y1,'bo-.');
xlabel('x values');
ylabel('y values');
% Plot y2 on the bottom
subplot(2,1,2);
y2 = x + 2;
plot(x, y2, 'g+:');
Figure
Example
x = [-3 -2 -1 0 1 2 3];
y1 = (x.^2) -1;
% Plot y1 in the 1st Figure
plot(x, y1,'bo-.');
xlabel('x values');
ylabel('y values');
% Plot y2 in the 2nd Figure
figure
y2 = x + 2;
plot(x, y2, 'g+:');
Surface Plot
x = 0:0.1:2;
y = 0:0.1:2;
[xx, yy] = meshgrid(x,y);
zz=sin(xx.^2+yy.^2);
surf(xx,yy,zz)
xlabel('X axes')
ylabel('Y axes')
2D line plot
1
>> x=0:0.05:5; 0.8
0.4
>> plot(x,y); 0.2
amplitude
>> xlabel('time'); 0
-0.2
-0.8
>>ylabel('amplitude'); -1
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time
Bar plot
>> x=-2.9:0.2:2.9; 1
0.9
>> bar(x,exp(-x.*x)); 0.8
>> 0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
-3 -2 -1 0 1 2 3
Stairstep plot of sine wave
>> x=0:0.25:10; 1
0.8
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7 8 9 10
M-Files
» what
abc abc1
function out1=functionname(in1)
function out1=functionname(in1,in2,in3)
function [out1,out2]=functionname(in1,in2)
You should write this command at the beginning of the m-file and
you should save the m-file with a file name same as the function
name
Writing User Defined Functions
Examples
Write a function : out=squarer (A, ind)
Same Name
Writing User Defined Functions
Another function which takes an input array and returns the sum and product of its
elements as outputs
multiply.m output
function y = multiply(a,b) >> multiply(23,3)
y=a*b;
ans =
end
69
Function Example2
function1.m output
function [out1,out2] = function1(a,b) [a,b]=function1(2,4)
out1=sin(a); a=
out2=sin(b); 0.9093
end
b=
-0.7568
Cluster Analysis?
Finding groups of objects such that the objects in a group will
be similar (or related) to one another and different from (or
unrelated to) the objects in other groups
Clustering techniques
Clustering
Partitional Hierarchical
k-mean agglomerative
divisive
Hierarchical clustering
Algorithm Description
1.
0.075
0.07
0.065
2. 0.06
0.055
0.05
2 28 7 24 27 21 18 12 6 9 15 26 10 3 8 29 5 17 22 13 30 16 1 23 4 11 20 19 25 14
3.
Hierarchical clustering using matlab
X=meas 0.07
Y = pdist(X) 0.065
z=linkage(Y) 0.06
0.055
dendrogram(z) 0.05
2 28 7 24 27 21 18 12 6 9 15 26 10 3 8 29 5 17 22 13 30 16 1 23 4 11 20 19 25 14
Hierarchical clustering using matlab
7
load fisheriris 6
X=meas 5
Y = pdist(X) 3
z=linkage(Y) 2
dendrogram(z) 4.5
4
7.5
8
3.5 7
hidx=cluster(z,'MaxClust',3) 3
2.5 5
5.5
6
6.5
4.5
ptsymb = {'bs','r^','md','go','c+'}; 2 4
for i=1:max(hidx)
clust=find(hidx==i)
plot3(X(clust,1),X(clust,2),X(clust,3),ptsymb{i});
hold on;
end
Thanks