Covariance & Correlation Coefficient calculation in MATLAB
Code:
clc
clear all
close all
x=input('Enter the data points:');
a1=mean(x);
z=[];
for i=1:length(x)
z=[z (x(i)-a1)^2];
end
s1=sum(z);
s1=s1/(length(x)-1);
s1=sqrt(s1);
y=input('Enter the data points:');
a2=mean(y);
z=[];
for i=1:length(y)
z=[z (y(i)-a2)^2];
end
s2=sum(z);
s2=s2/(length(x)-1);
s2=sqrt(s2);
x=x-a1;
y=y-a2;
ak=x*y';
cova=ak/(length(x)-1);
corr=cova/(s1*s2);
clc
clear all
close all
x=input('Enter the data points:');
a1=mean(x);
z=[];
for i=1:length(x)
z=[z (x(i)-a1)^2];
end
s1=sum(z);
s1=s1/(length(x)-1);
s1=sqrt(s1);
y=input('Enter the data points:');
a2=mean(y);
z=[];
for i=1:length(y)
z=[z (y(i)-a2)^2];
end
s2=sum(z);
s2=s2/(length(x)-1);
s2=sqrt(s2);
x=x-a1;
y=y-a2;
ak=x*y';
cova=ak/(length(x)-1);
corr=cova/(s1*s2);
No comments