title: Auto-covariance and Cross Covariance Function in Matlab without using imbuilt functions
body: <p><code>x</code> and <code>y</code> are 1x100000 vectors.</p>
  
  <p><img src="https://i.stack.imgur.com/QoteL.jpg" alt="Questions"></p>
  
  <p>I have calculated the mean and variance of <code>x</code> and <code>y</code>. When I want to calculate the autocovariance and cross covariance function the simulation lasts maybe 5 minutes because of my loops. It is not allowed to use <code>xcorr</code>, <code>xcov</code>, <code>mean</code>, <code>cov</code>, <code>var</code> etc.</p>
  
  <p>Please help me.</p>
  
  <p>Thanks in advance.</p>
  
  <pre><code>%%Mean of Vector x
  
  Nx=length(x);
  mx= sum(x)/Nx;
  
  %%Mean of Vector y
  
  Ny=length(y);
  my=sum(y)/Ny;
  
  %%Variance of x
  
  varx=0;
  
  for i=1:Nx
     varx=varx+(abs(x(i)-mx)^(2));
  end
  varx=varx/Nx;
  
  %%Variance of y
  
  vary=0;
  for j=1:Ny 
     vary=vary+(abs(y(j)-my)^(2));
  end
  vary=vary/Ny;
  
  
  %%Auto-Covariance function of x
  
  for k=1:Nx  
  
  Cxx(k)=0;
  
  for i=1:(Nx-k+1)    
     Cxx(k)=Cxx(k)+(x(i+k-1)-mx)*conj((x(i)-my));  
  end
  end
  
  %%Auto-Covariance function of y
  
  for s=1:Ny  
  
  Cyy(s)=0;
  
  for j=1:(Ny-s+1)    
     Cyy(s)=Cyy(s)+(y(j+s-1)-my)*conj((y(j)-mx));  
  end
  end
  </code></pre>