I was very pleased today to find out that some people made a library which makes it possible to utilize MATLAB code directly in the iPython Notebook, which this webpage is written in. As you see in the In[134] the library is called pymatbridge. For me personally this is advantageous because most of the material at this semester is based on utilizing MATLAB libraries, so now this webpage is directly compatible with my daily work, with \(\LaTeX\), MATLAB and Python.
Importing pymatbridge and starting MATLAB
This is how to enable the bridge.
import pymatbridge as mpb
ip = get_ipython()
mpb.load_ipython_extension(ip)
Example of plotting
%%matlab
a= linspace(0,6*pi,1000);
plot(sin(a)+0.25*sin(20*a+0.5*pi))
title('Test Plot of Amplitude Modulation Signal','fontsize',18)
xlabel('n','fontsize',16)
ylabel('f(n)','fontsize',16)
Matrix Syntax and Covariance Matrices
Covariance Matrix
Now we will show how to calculate the covariance matrix by matrix multiplications.
%%matlab
format compact
X = [1+j 2-j;3+j 4-j]
[m,n] = size(X);
ONE = ones(1,m)
X_bar = mean(X)
X_bar = (X_bar'*ONE)'
R = X-X_bar
Covariance = (R'*R)/(m-1)
And basically, the correlation matrix is a scaled version of the covariance matrix. It is scaled to the sample variance as follows: Firstly, we define the covariance as \(\sigma\) and the correlation \(C\)
Correlation Between Two Vectors
Now we will do a quick example of plotting the correlation between 2 measurements of 400 random samples. Therefore, they do show no correlation.
%%matlab
X=rand(400,2);
x_bar = mean(X)'
x_covar = cov(X)
x_corr = corr(X)
R = corrcoef(X)
Sn = x_covar; fprintf('\n------------------------------------------------\n');
figure
a=1;b=2;
scatterhist(X(:,a),X(:,b),100);
xlabel(sprintf('Column %1.0f',a),'Fontsize',15); ylabel(sprintf('Column %1.0f',b),'fontsize',15);