I have two vectors:
A_1 =
10
200
7
150
A_2 =
0.001
0.450
0.0007
0.200
I would like to know if there is correlation between these two vectors.
I could subtract to each value the mean of the vector and than do:
A_1' * A_2
Are there any better ways?
Correlation in R can be calculated using cor() function. In R, Cor() function is used to calculate correlation among vectors, Matrices and data frames.
Complete correlation between two variables is expressed by either + 1 or -1. When one variable increases as the other increases the correlation is positive; when one decreases as the other increases it is negative. Complete absence of correlation is represented by 0.
R = corrcoef( A ) returns the matrix of correlation coefficients for A , where the columns of A represent random variables and the rows represent observations. R = corrcoef( A , B ) returns coefficients between two random variables A and B .
If x(n), y(n) and z(n) are the samples of the signals, the correlation coefficient between x and y is given by Sigma x(n) * y(n) divided by the root of [Sigma x(n)^2 * y(n)^2], where ' * ' denotes simple multiplication and ^2 denotes squaring. The summation is taken over all the samples of the signals.
Given:
A_1 = [10 200 7 150]';
A_2 = [0.001 0.450 0.007 0.200]';
(As others have already pointed out) There are tools to simply compute correlation, most obviously corr
:
corr(A_1, A_2); %Returns 0.956766573975184 (Requires stats toolbox)
You can also use base Matlab's corrcoef
function, like this:
M = corrcoef([A_1 A_2]): %Returns [1 0.956766573975185; 0.956766573975185 1];
M(2,1); %Returns 0.956766573975184
Which is closely related to the cov
function:
cov([condition(A_1) condition(A_2)]);
As you almost get to in your original question, you can scale and adjust the vectors yourself if you want, which gives a slightly better understanding of what is going on. First create a condition function which subtracts the mean, and divides by the standard deviation:
condition = @(x) (x-mean(x))./std(x); %Function to subtract mean AND normalize standard deviation
Then the correlation appears to be (A_1 * A_2)/(A_1^2), like this:
(condition(A_1)' * condition(A_2)) / sum(condition(A_1).^2); %Returns 0.956766573975185
By symmetry, this should also work
(condition(A_1)' * condition(A_2)) / sum(condition(A_2).^2); %Returns 0.956766573975185
And it does.
I believe, but don't have the energy to confirm right now, that the same math can be used to compute correlation and cross correlation terms when dealing with multi-dimensiotnal inputs, so long as care is taken when handling the dimensions and orientations of the input arrays.
Try xcorr
, it's a built-in function in MATLAB for cross-correlation:
c = xcorr(A_1, A_2);
However, note that it requires the Signal Processing Toolbox installed. If not, you can look into the corrcoef
command instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With