Matlab (Matrix laboratory)
The source file (function file) has extension .m (e.g. script.m)
#To save workspace for later use
save script.m
load script.m
The file with suffix .mat has data matrix (e.g data.mat, golubsmall.mat)
# The mat file splits into one with matrix and another with label in grid format (cell array) (row * column)
load ('data.mat')
e.g. dimensionality (16 row x 24 columns)
ndata (70 row x 20 columns)
The source file reads .dat file (containing data)
Comment (%)
###################################################
Data type determination using the functions
x = 5
isinteger(x)
isfloat(x)
isvector(x)
isscalar(x)
isnumeric(x)
ischar(x)
isreal(x)
isobject(x)
class(x)
Arithmetic calculations
2 + 7
6 ^ 2
sin(pi)
8/0
381 * 10.7
x = 4;
y = x + 8
-----------------------
Answers
y = 12
###################################################
% Variable assignment
x = 6
x = sqrt(81)
sqrt(488)
sqrt(48);396/ans
x = 4 * 6;
y = x * 3.2
-----------------------
Answers
% Multiple variable assignment
a = 3; b = 2;
c = a + b
d = a / b
e = a ** b
-----------------------
Answers
%Final velocity calculation
initial_velocity = 0;
acceleration = 9.8;
time = 30;
final_velocity = initial_velocity + acceleration * time
final_velocity = 294
###################################################
%Format of result display
x = 5 + 8/6 + 2 ^ 2.5
format long
x = 5 + 8/6 + 2 ^ 2.5
x = 23.8967452346
format short
x = 23.897
###################################################
%The command format bank will keep only two digits after decimal point
daily_wage = 17.10;
weekly_wage = daily_wage * 5
format bank
daily_wage = 17.10;
weekly_wage = daily_wage * 5
%The command format short e will express in exponential notation
5.2 * 1.7
format short e
5.2 * 1.7
x = pi
format long e
x = pi
Vectors
row vector
r = [3 6 7 9]
b = [4 5 3 7];
new = a + b
column vector
c = [4; 6; 8; 9]
matrix vector
matrix = [1 5 7; 4 7 3; 2 9 6; 7 1 4]
Reference to the vector elements and sub-strings
vector = [ 1; 5; 8; 7; 4; 3];
vector(4)
vector(:)
vector = [4 8 5 9 3 2];
parts = vector(2:5)
Matrix manipulation (vector (3,4) here means element at the intersection of row 3 and column 4).
vector = [3 2 8 4 ; 8 3 5 7 ; 4 7 3 6]
vector(3,4)
vector1 = [3 2 8 4 ; 8 3 5 7 ; 4 7 3 6]
vector2 = vector1(3,:)
vector2 = vector1(:,3)
vector2 = vector1(:,:)
vector2 = vector1(1:2,1:2)
vector2 = vector1(2:3,1:2)
Array generation
Zero-based array
zeros (3) %matrix of 3 row, 3 column
One-based array
ones(2,4) %matrix of 2 row, 4 column
Identity (diagonal)array
eye(3) %matrix of 3 row, 3 column
Random array
rand(2, 3) %It differs in each execution
Magic array %Row, column or diagonal sum is always same
magic(3)
Calculations
People = 12;
Cattle = 14;
Horse = 2;
Dogs = 3;
Cats = 4;
Total = People + Cattle + Horse + Dogs+...
+ Cats;
disp(Total);
35
###################################################
%Generates nearest rational expression
2.57 * 3.18
format rat
2.57 * 3.18
Common functions
rand():Returns an array of random numbers
size (rand ()): Gives size of a ransom array
zeros(): Creates a matrix of zeros
abs(): Returns absolute value of each element in an array
exp(): Returns exponential of each element in an array
hold off: Resets axis properties to default
hold on: Retains current plot so that subsequent graphs can be added
hold all: Holds the properties
plot (): Creates graph
fplot: To plot a function between specified limits
subplot (): It divides current figure to rectangular panes
format: To control display format for output
round(): Rounds of numbers to next integer
clear: clears
close all: closes
clc; resets
dim: matrix dimensions
delx:
gcf: returns the current figure handle
realsqrt: Returns square root of each element of the array
num2str: Converts numeric array to string
###################################################
% Takes three matrix and generates plot in pdf or eps format
x = [1 2 3 4 5];
y1 = [.23 .45 .78 .35 .65];
y2 = [.45 .56 .23 .34 .28];
%y3 = [.48 .53 .78 .23 .49];
semilogy(x,y1,'-bo;y1;',x,y2,'-kx;y2;');
%semilogy(x,y1,'-bo;y1;',x,y2,'-kx;y2;', x,y3,'-kx;y3;');
title('Plot chart');
xlabel('X Axis');
ylabel('Y Axis');
print -dpdf chart.pdf
--------------------------------------
x = [1 2 3 4 5 ];
y1 = [.34 .78 .89 .56 .34];
y2 = [.23 .56 .78 .54 .36];
semilogy(x,y1,'-bo;y1;',x,y2,'-kx;y2;');
title('Plot title');
xlabel('X Axis');
ylabel('Y Axis');
print -deps graph.eps
###################################################
%To generate vectors U (uniformly-distributed) and N(normally-distributed) of particular length,
% range, mean an SD (Suppose length=100, range is 0,1, mean =0.5, SD = 0.05)
U Vector =rand (length, upper limit)
N Vector =normrand (mean, SD, length, upper limit)
U=rand(100,1);
N=normrnd(0.5,0.05,100,1);
%N=0.5+ 0.05.*randn(100, 1);
###################################################
% To estimate probability density function (pdf ) when window width is given
%Suppose SD (s=0.005), window width is 0.01
%Using a: increment:b in increments size of 0.001
% x=[a:i:b], where i is increment and a=0 and b=1
x=0:0.001:1;
%Initializing height of each kernel and total height after summation
% Length of largest array = L = length (x)
height=zeros(1,length(x));
h = zeros(1,length(x));
###################################################
% To estimate of pdf for U vector
l = length(U);
for i=1:l
%x(i)=a+delx*(i-1)where index of x has to be an integer > 0
m=round((U(i)-s)*100+1);
%x(i)=a+delx*(i-1) where index of x has to be an integer < 100
n=round((U(i)+s)*100+1);
if m<1
m=1;
end
if n>101
n=101;
end
height(m+1:n)=1;
%Adding all kernel heights to total height and storing them in 'h'
h = h + height;
%Re-initializing height after calculating for each kernel
height=zeros(1,length(x));
end
%y axis is normalized to the number of data points i.e 1000 here
h=(h/100)*100;
-----------------------------------------------------------------------
%Estimation of pdf for N vector
height=zeros(1,length(x));
%Initializing total height after adding the kernels
j = zeros(1,length(x));
l = length(N);
for i=1:l
%x(i)=a+delx*(i-1)where index of x has to be an integer > 0
m=round((N(i)-s)*100+1);
%x(i)=a+delx*(i-1) where index of x has to be an integer < 100
n=round((N(i)+s)*100+1);
if m<1
m=1;
end
if n>100
n=100;
end
%Another precaution
height(m+1:n)=1;
%Adding all kernel heights to total height and storing them in 'h'
j = j + height;
%Re-initializing height after calculating for each kernel
height=zeros(1,length(x));
end
%y axis is normalized to the number of data points i.e 100 here
j=(j/100)*100;
figure(1)
%num2str function is useful for labeling plots with numeric values.
%To get unbiased estimate of s
name=['Parzen Density Estimation with Uniform kernels ' num2str(s*2)];
set(gcf,'name',name);
%plot for pdf of U
plot(x,h,'g','linewidth',3);
%hold on retains the current graph and adds another graph to it
hold on
%plot for pdf of N
plot(x,j,'r','linewidth',3)
title(name);
%xlabel(str) labels the x-axis of the current axes with the string, str
%ylabel(str) labels the y-axis of the current axes with the string, str
xlabel('Random numbers (U,N)');
ylabel('Kernel density (x,h/j)');
print(gcf,'-djpeg',[name '.jpg']);
%Estimation of pdf when window width is 0.05
s=0.025;
x=0:0.001:1;
%Initializing height of each kernel and total height after summation
height=zeros(1,length(x));
h = zeros(1,length(x));
%Estimation of pdf for U vector
l = length(U);
for i=1:l
%x(i)=a+delx*(i-1)where index of x has to be an integer > 0
m=round((U(i)-s)*1000+1);
%x(i)=a+delx*(i-1) where index of x has to be an integer < 1000
n=round((U(i)+s)*1000+1);
if m<1
m=1;
end
if n>1001
n=1001;
end
%Another precaution
height(m+1:n)=1;
%Adding all kernel heights to total height and storing them in 'h'
h = h + height;
%Re-initializing height after calculating for each kernel
height=zeros(1,length(x));
end
%y axis is normalized to the number of data points i.e 1000 here
h=(h/1000)*20;
-------------------------------------------------------------
%Estimation of pdf for N vector
height=zeros(1,length(x));
j = zeros(1,length(x));
l = length(N);
for i=1:l
%x(i)=a+delx*(i-1)where index of x has to be an integer > 0
m=round((N(i)-s)*100+1);
%x(i)=a+delx*(i-1) where index of x has to be an integer < 100
n=round((N(i)+s)*100+1);
if m<1
m=1;
end
if n>101
n=101;
end
%Another precaution
height(m+1:n)=1;
%Adding all kernel heights to total height and storing them in 'h'
j = j + height;
%Re-initializing height after calculating for each kernel
height=zeros(1,length(x));
end
%y axis is normalized to the number of data points i.e 1000 here
j=(j/1000)*20;
figure(2)
%num2str function is useful for labeling plots with numeric values.
%To get unbiased estimate of s
name =['Parzen Density Estimation with Uniform Kernels ' num2str(s*2)];
set(gcf,'name',name);
%plot for pdf of U
plot(x,h,'g','linewidth',3)
%hold on retains the current graph and adds another graph to it
hold on
%plot for pdf of N
plot(x,j,'r','linewidth',3)
title(name);
%xlabel(str) labels the x-axis of the current axes with the string, str
%ylabel(str) labels the y-axis of the current axes with the string, str
xlabel('Random numbers (U,N)');
ylabel('Kernel density (x,h/j)');
print(gcf,'-djpeg',[name '.jpg']);
%Estimation of pdf when window width is 0.1
%pdf for U vector
s=0.05; x=0:0.001:1; h=zeros(1,length(x)); l = length(U);
for i=1:l
%x(i)=a+delx*(i-1)where index of x has to be an integer > 0
m=round((U(i)-s)*1000+1);
%x(i)=a+delx*(i-1) where index of x has to be an integer < 1000
n=round((U(i)+s)*1000+1);
if m<1
m=1;
end
if n>1001
n=1001;
end
%Another precaution
height(m+1:n)=1;
%Adding all kernel heights to total height and storing them in 'h'
h = h + height;
%Re-initializing height after calculating for each kernel
height=zeros(1,length(x));
end
%y axis is normalized to the number of data points i.e 1000 here
h=(h/1000)*20;
%pdf for N vector
height=zeros(1,length(x));
j = zeros(1,length(x));
l = length(N);
for i=1:l
%x(i)=a+delx*(i-1)where index of x has to be an integer > 0
m=round((N(i)-s)*1000+1);
%x(i)=a+delx*(i-1) where index of x has to be an integer < 1000
n=round((N(i)+s)*1000+1);
if m<1
m=1;
end
if n>1001
n=1001;
end
%Another precaution
height(m+1:n)=1;
%Adding all kernel heights to total height and storing them in 'h'
j = j + height;
%Re-initializing height after calculating for each kernel
height=zeros(1,length(x));
end
%y axis is normalized to the number of data points i.e 1000 here
j=(j/1000).*(1/(s*2));
figure(3)
%num2str function is useful for labeling plots with numeric values.
%To get unbiased estimate of s
name=['Parzen Density Estimation with Uniform kernels ' num2str(s*2)];
set(gcf,'name',name);
%plot for pdf of U
plot(x,h,'g','linewidth',3)
%hold on retains the current graph and adds another graph to it
hold on
%plot for pdf of N
plot(x,j,'r','linewidth',3)
title(name);
%xlabel(str) labels the x-axis of the current axes with the string, str
%ylabel(str) labels the y-axis of the current axes with the string, str
xlabel('Random numbers (U,N)');
ylabel('Kernel density (x,h/j)');
print(gcf,'-djpeg',[name '.jpg']);
-------------------------------------------------------------
%Standard deviation vector is v and standard deviation is s
v=[0.01 0.05 0.1];
%This for loop traverses v and calculates the normal distribution for each s
for i=1:length(v)
x=0:0.0001:1;
height=zeros(1,length(x));
j=zeros(1,length(x));
h=zeros(1,length(x));
l=length(N);
u=length(U);
%For loop traverses N and determines the height of each kernel
for p=1:l
%Normal distribution equation
height=(1/(realsqrt(2*pi)*v(i))).*exp((-(x-N(p)).^2)/(2*v(i)^2));
%Adding each kernel height to the total height
j=j+height;
height=zeros(1,length(x));
end
j=j/100;
%For loop traverses U and determines the height of each kernel
for q=1:u
%Normal distribution equation
height1=(1/(realsqrt(2*pi)*v(i))).*exp((-(x-U(q)).^2)/(2*v(i)^2));
%Adding each kernel height to the total height
h=h+height1;
%Re-initializing each kernel height
height1=zeros(1,length(x));
end
%Normalizing the y-axis to account for the 100 points
h=h/100;
figure(4)
name=['Parzen Density Estimation with Normal Kernels ' num2str(v(i))];
set(gcf,'name',name);
plot(x,h,'g','linewidth',2)
hold on
plot(x,j,'r','linewidth',2)
title(name);
xlabel('Random numbers (U,N)');
ylabel('Kernel density (x,h/j)');
print(gcf,'-djpeg',[name '.jpg']);
end
#####################
Suppose
a = 3
b = 7
who
a b
Use of whos command will tell the variable attributes as size, bytes and class
whos
The source file (function file) has extension .m (e.g. script.m)
#To save workspace for later use
save script.m
load script.m
The file with suffix .mat has data matrix (e.g data.mat, golubsmall.mat)
# The mat file splits into one with matrix and another with label in grid format (cell array) (row * column)
load ('data.mat')
e.g. dimensionality (16 row x 24 columns)
ndata (70 row x 20 columns)
The source file reads .dat file (containing data)
Comment (%)
###################################################
Data type determination using the functions
x = 5
isinteger(x)
isfloat(x)
isvector(x)
isscalar(x)
isnumeric(x)
ischar(x)
isreal(x)
isobject(x)
class(x)
ans = 0
ans = 1
ans = 1
ans = 1
ans = 1
ans = 0
ans = 1
ans = 0
ans = double
###################################################Arithmetic calculations
2 + 7
6 ^ 2
sin(pi)
8/0
381 * 10.7
x = 4;
y = x + 8
-----------------------
Answers
ans = 9
ans = 36
ans = 1.2246e-16
warning: division by zero
ans = Inf
ans = 4076.7
y = 12
###################################################
% Variable assignment
x = 6
x = sqrt(81)
sqrt(488)
sqrt(48);396/ans
x = 4 * 6;
y = x * 3.2
-----------------------
Answers
x = 6
x = 9
ans = 22.091
ans = 57.158
y = 76.800
###################################################% Multiple variable assignment
a = 3; b = 2;
c = a + b
d = a / b
e = a ** b
-----------------------
Answers
c = 5
d = 1.5000
e = 9
################################################### %Final velocity calculation
initial_velocity = 0;
acceleration = 9.8;
time = 30;
final_velocity = initial_velocity + acceleration * time
final_velocity = 294
###################################################
%Format of result display
x = 5 + 8/6 + 2 ^ 2.5
format long
x = 5 + 8/6 + 2 ^ 2.5
x = 11.990
x = 11.9901875828257
x = 23.8967452346
format short
x = 23.897
###################################################
%The command format bank will keep only two digits after decimal point
daily_wage = 17.10;
weekly_wage = daily_wage * 5
format bank
daily_wage = 17.10;
weekly_wage = daily_wage * 5
weekly_wage = 85.500
weekly_wage = 85.50
################################################### %The command format short e will express in exponential notation
5.2 * 1.7
format short e
5.2 * 1.7
ans = 8.8400
ans = 8.8400e+00
x = pi
format long e
x = pi
x = 3.1416
x = 3.14159265358979e+00
################################################### Vectors
row vector
r = [3 6 7 9]
r =
3 6 7 9
a = [2 7 8 6];b = [4 5 3 7];
new = a + b
new =
6 12 11 13
column vector
c = [4; 6; 8; 9]
c =
4
6
8
9
matrix vector
matrix = [1 5 7; 4 7 3; 2 9 6; 7 1 4]
matrix =
1 5 7
4 7 3
2 9 6
7 1 4
Reference to the vector elements and sub-strings
vector = [ 1; 5; 8; 7; 4; 3];
vector(4)
vector(:)
vector = [4 8 5 9 3 2];
parts = vector(2:5)
ans = 7
ans =
1
5
8
7
4
3
parts =
8 5 9 3
Matrix manipulation (vector (3,4) here means element at the intersection of row 3 and column 4).
vector = [3 2 8 4 ; 8 3 5 7 ; 4 7 3 6]
vector(3,4)
vector =
3 2 8 4
8 3 5 7
4 7 3 6
ans = 6
vector1 = [3 2 8 4 ; 8 3 5 7 ; 4 7 3 6]
vector1 =
3 2 8 4
8 3 5 7
4 7 3 6
vector2 = vector1(3,:)
vector2 =
4 7 3 6
vector2 = vector1(:,3)
vector2 =
8
5
3
vector2 = vector1(:,:)
vector2 =
3 2 8 4
8 3 5 7
4 7 3 6
vector2 = vector1(1:2,1:2)
vector2 =
3 2
8 3
vector2 = vector1(2:3,1:2)
vector2 =
8 3
4 7
################################################### Array generation
Zero-based array
zeros (3) %matrix of 3 row, 3 column
ans =
0 0 0
0 0 0
0 0 0
zeros(5,4) %matrix of 5 row, 4 column
ans =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
One-based array
ones(2,4) %matrix of 2 row, 4 column
ans =
1 1 1 1
1 1 1 1
Identity (diagonal)array
eye(3) %matrix of 3 row, 3 column
Diagonal Matrix
1 0 0
0 1 0
0 0 1
Random array
rand(2, 3) %It differs in each execution
ans =
0.029232 0.434291 0.721927
0.837602 0.117745 0.356975
Magic array %Row, column or diagonal sum is always same
magic(3)
ans =
8 1 6
3 5 7
4 9 2
################################################### Calculations
People = 12;
Cattle = 14;
Horse = 2;
Dogs = 3;
Cats = 4;
Total = People + Cattle + Horse + Dogs+...
+ Cats;
disp(Total);
35
###################################################
%Generates nearest rational expression
2.57 * 3.18
format rat
2.57 * 3.18
ans = 8.1726
ans = 40863/5000
################################################### Common functions
rand():Returns an array of random numbers
size (rand ()): Gives size of a ransom array
zeros(): Creates a matrix of zeros
abs(): Returns absolute value of each element in an array
exp(): Returns exponential of each element in an array
hold off: Resets axis properties to default
hold on: Retains current plot so that subsequent graphs can be added
hold all: Holds the properties
plot (): Creates graph
fplot: To plot a function between specified limits
subplot (): It divides current figure to rectangular panes
format: To control display format for output
round(): Rounds of numbers to next integer
clear: clears
close all: closes
clc; resets
dim: matrix dimensions
delx:
gcf: returns the current figure handle
realsqrt: Returns square root of each element of the array
num2str: Converts numeric array to string
###################################################
% Takes three matrix and generates plot in pdf or eps format
x = [1 2 3 4 5];
y1 = [.23 .45 .78 .35 .65];
y2 = [.45 .56 .23 .34 .28];
%y3 = [.48 .53 .78 .23 .49];
semilogy(x,y1,'-bo;y1;',x,y2,'-kx;y2;');
%semilogy(x,y1,'-bo;y1;',x,y2,'-kx;y2;', x,y3,'-kx;y3;');
title('Plot chart');
xlabel('X Axis');
ylabel('Y Axis');
print -dpdf chart.pdf
--------------------------------------
x = [1 2 3 4 5 ];
y1 = [.34 .78 .89 .56 .34];
y2 = [.23 .56 .78 .54 .36];
semilogy(x,y1,'-bo;y1;',x,y2,'-kx;y2;');
title('Plot title');
xlabel('X Axis');
ylabel('Y Axis');
print -deps graph.eps
###################################################
%To generate vectors U (uniformly-distributed) and N(normally-distributed) of particular length,
% range, mean an SD (Suppose length=100, range is 0,1, mean =0.5, SD = 0.05)
U Vector =rand (length, upper limit)
N Vector =normrand (mean, SD, length, upper limit)
U=rand(100,1);
N=normrnd(0.5,0.05,100,1);
%N=0.5+ 0.05.*randn(100, 1);
###################################################
% To estimate probability density function (pdf ) when window width is given
%Suppose SD (s=0.005), window width is 0.01
%Using a: increment:b in increments size of 0.001
% x=[a:i:b], where i is increment and a=0 and b=1
x=0:0.001:1;
%Initializing height of each kernel and total height after summation
% Length of largest array = L = length (x)
height=zeros(1,length(x));
h = zeros(1,length(x));
###################################################
% To estimate of pdf for U vector
l = length(U);
for i=1:l
%x(i)=a+delx*(i-1)where index of x has to be an integer > 0
m=round((U(i)-s)*100+1);
%x(i)=a+delx*(i-1) where index of x has to be an integer < 100
n=round((U(i)+s)*100+1);
if m<1
m=1;
end
if n>101
n=101;
end
height(m+1:n)=1;
%Adding all kernel heights to total height and storing them in 'h'
h = h + height;
%Re-initializing height after calculating for each kernel
height=zeros(1,length(x));
end
%y axis is normalized to the number of data points i.e 1000 here
h=(h/100)*100;
-----------------------------------------------------------------------
%Estimation of pdf for N vector
height=zeros(1,length(x));
%Initializing total height after adding the kernels
j = zeros(1,length(x));
l = length(N);
for i=1:l
%x(i)=a+delx*(i-1)where index of x has to be an integer > 0
m=round((N(i)-s)*100+1);
%x(i)=a+delx*(i-1) where index of x has to be an integer < 100
n=round((N(i)+s)*100+1);
if m<1
m=1;
end
if n>100
n=100;
end
%Another precaution
height(m+1:n)=1;
%Adding all kernel heights to total height and storing them in 'h'
j = j + height;
%Re-initializing height after calculating for each kernel
height=zeros(1,length(x));
end
%y axis is normalized to the number of data points i.e 100 here
j=(j/100)*100;
figure(1)
%num2str function is useful for labeling plots with numeric values.
%To get unbiased estimate of s
name=['Parzen Density Estimation with Uniform kernels ' num2str(s*2)];
set(gcf,'name',name);
%plot for pdf of U
plot(x,h,'g','linewidth',3);
%hold on retains the current graph and adds another graph to it
hold on
%plot for pdf of N
plot(x,j,'r','linewidth',3)
title(name);
%xlabel(str) labels the x-axis of the current axes with the string, str
%ylabel(str) labels the y-axis of the current axes with the string, str
xlabel('Random numbers (U,N)');
ylabel('Kernel density (x,h/j)');
print(gcf,'-djpeg',[name '.jpg']);
%Estimation of pdf when window width is 0.05
s=0.025;
x=0:0.001:1;
%Initializing height of each kernel and total height after summation
height=zeros(1,length(x));
h = zeros(1,length(x));
%Estimation of pdf for U vector
l = length(U);
for i=1:l
%x(i)=a+delx*(i-1)where index of x has to be an integer > 0
m=round((U(i)-s)*1000+1);
%x(i)=a+delx*(i-1) where index of x has to be an integer < 1000
n=round((U(i)+s)*1000+1);
if m<1
m=1;
end
if n>1001
n=1001;
end
%Another precaution
height(m+1:n)=1;
%Adding all kernel heights to total height and storing them in 'h'
h = h + height;
%Re-initializing height after calculating for each kernel
height=zeros(1,length(x));
end
%y axis is normalized to the number of data points i.e 1000 here
h=(h/1000)*20;
-------------------------------------------------------------
%Estimation of pdf for N vector
height=zeros(1,length(x));
j = zeros(1,length(x));
l = length(N);
for i=1:l
%x(i)=a+delx*(i-1)where index of x has to be an integer > 0
m=round((N(i)-s)*100+1);
%x(i)=a+delx*(i-1) where index of x has to be an integer < 100
n=round((N(i)+s)*100+1);
if m<1
m=1;
end
if n>101
n=101;
end
%Another precaution
height(m+1:n)=1;
%Adding all kernel heights to total height and storing them in 'h'
j = j + height;
%Re-initializing height after calculating for each kernel
height=zeros(1,length(x));
end
%y axis is normalized to the number of data points i.e 1000 here
j=(j/1000)*20;
figure(2)
%num2str function is useful for labeling plots with numeric values.
%To get unbiased estimate of s
name =['Parzen Density Estimation with Uniform Kernels ' num2str(s*2)];
set(gcf,'name',name);
%plot for pdf of U
plot(x,h,'g','linewidth',3)
%hold on retains the current graph and adds another graph to it
hold on
%plot for pdf of N
plot(x,j,'r','linewidth',3)
title(name);
%xlabel(str) labels the x-axis of the current axes with the string, str
%ylabel(str) labels the y-axis of the current axes with the string, str
xlabel('Random numbers (U,N)');
ylabel('Kernel density (x,h/j)');
print(gcf,'-djpeg',[name '.jpg']);
%Estimation of pdf when window width is 0.1
%pdf for U vector
s=0.05; x=0:0.001:1; h=zeros(1,length(x)); l = length(U);
for i=1:l
%x(i)=a+delx*(i-1)where index of x has to be an integer > 0
m=round((U(i)-s)*1000+1);
%x(i)=a+delx*(i-1) where index of x has to be an integer < 1000
n=round((U(i)+s)*1000+1);
if m<1
m=1;
end
if n>1001
n=1001;
end
%Another precaution
height(m+1:n)=1;
%Adding all kernel heights to total height and storing them in 'h'
h = h + height;
%Re-initializing height after calculating for each kernel
height=zeros(1,length(x));
end
%y axis is normalized to the number of data points i.e 1000 here
h=(h/1000)*20;
%pdf for N vector
height=zeros(1,length(x));
j = zeros(1,length(x));
l = length(N);
for i=1:l
%x(i)=a+delx*(i-1)where index of x has to be an integer > 0
m=round((N(i)-s)*1000+1);
%x(i)=a+delx*(i-1) where index of x has to be an integer < 1000
n=round((N(i)+s)*1000+1);
if m<1
m=1;
end
if n>1001
n=1001;
end
%Another precaution
height(m+1:n)=1;
%Adding all kernel heights to total height and storing them in 'h'
j = j + height;
%Re-initializing height after calculating for each kernel
height=zeros(1,length(x));
end
%y axis is normalized to the number of data points i.e 1000 here
j=(j/1000).*(1/(s*2));
figure(3)
%num2str function is useful for labeling plots with numeric values.
%To get unbiased estimate of s
name=['Parzen Density Estimation with Uniform kernels ' num2str(s*2)];
set(gcf,'name',name);
%plot for pdf of U
plot(x,h,'g','linewidth',3)
%hold on retains the current graph and adds another graph to it
hold on
%plot for pdf of N
plot(x,j,'r','linewidth',3)
title(name);
%xlabel(str) labels the x-axis of the current axes with the string, str
%ylabel(str) labels the y-axis of the current axes with the string, str
xlabel('Random numbers (U,N)');
ylabel('Kernel density (x,h/j)');
print(gcf,'-djpeg',[name '.jpg']);
-------------------------------------------------------------
%Standard deviation vector is v and standard deviation is s
v=[0.01 0.05 0.1];
%This for loop traverses v and calculates the normal distribution for each s
for i=1:length(v)
x=0:0.0001:1;
height=zeros(1,length(x));
j=zeros(1,length(x));
h=zeros(1,length(x));
l=length(N);
u=length(U);
%For loop traverses N and determines the height of each kernel
for p=1:l
%Normal distribution equation
height=(1/(realsqrt(2*pi)*v(i))).*exp((-(x-N(p)).^2)/(2*v(i)^2));
%Adding each kernel height to the total height
j=j+height;
height=zeros(1,length(x));
end
j=j/100;
%For loop traverses U and determines the height of each kernel
for q=1:u
%Normal distribution equation
height1=(1/(realsqrt(2*pi)*v(i))).*exp((-(x-U(q)).^2)/(2*v(i)^2));
%Adding each kernel height to the total height
h=h+height1;
%Re-initializing each kernel height
height1=zeros(1,length(x));
end
%Normalizing the y-axis to account for the 100 points
h=h/100;
figure(4)
name=['Parzen Density Estimation with Normal Kernels ' num2str(v(i))];
set(gcf,'name',name);
plot(x,h,'g','linewidth',2)
hold on
plot(x,j,'r','linewidth',2)
title(name);
xlabel('Random numbers (U,N)');
ylabel('Kernel density (x,h/j)');
print(gcf,'-djpeg',[name '.jpg']);
end
#####################
Suppose
a = 3
b = 7
who
a b
Use of whos command will tell the variable attributes as size, bytes and class
whos
Attr Name Size Bytes Class
==== ==== ==== ===== =====
a 1x1 8 double
b 1x1 8 double
#####################
No comments:
Post a Comment