Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab correlation between two matrices

Basically I have two matrices, something like this:

  > Matrix A (100 rows x 2 features)

  Height - Weight
  1.48      75
  1.55      65
  1.60      70
  etc...

And Matrix B (same dimension of Matrix A but with different values of course)

I would like to understand if there is some correlation between Matrix A and Matrix B, which strategy do you suggest me?

like image 488
dynamic Avatar asked Dec 19 '12 19:12

dynamic


People also ask

How do you find the correlation between two matrices?

To compute the cross-correlation of two matrices, compute and sum the element-by-element products for every offset of the second matrix relative to the first. With several caveats, this can be used to calculate the offset required to get 2 matrices of related values to overlap.

How do you show correlation in Matlab?

[ R , PValue ] = corrplot( Tbl ) plots the Pearson's correlation coefficients between all pairs of variables in the table or timetable Tbl , and also returns tables for the correlation matrix R and matrix of p-values PValue .

What is 2d cross-correlation?

Two dimensional correlation analysis is a mathematical technique that is used to study changes in measured signals. As mostly spectroscopic signals are discussed, sometime also two dimensional correlation spectroscopy is used and refers to the same technique.

What is a correlation matrix?

A correlation matrix is simply a table which displays the correlation coefficients for different variables. The matrix depicts the correlation between all the possible pairs of values in a table. It is a powerful tool to summarize a large dataset and to identify and visualize patterns in the given data.


2 Answers

The concept you are looking for is known as canonical correlation. It is a well developed bit of theory in the field of multivariate analysis. Essentially, the idea is to find a linear combination of the columns in your first matrix and a linear combination of the columns in your second matrix, such that the correlation between the two linear combinations is maximized.

This can be done manually using eigenvectors and eigenvalues, but if you have the statistics toolbox, then Matlab has already got it packaged and ready to go for you. The function is called canoncorr, and the documentation is here

A brief example of the usage of this function follows:

%# Set up some example data
CovMat = randi(5, 4, 4) + 20 * eye(4); %# Build a random covariance matrix
CovMat = (1/2) * (CovMat + CovMat'); %# Ensure random covriance matrix is symmetrix
X = mvnrnd(zeros(500, 4), CovMat); %# Simulate data using multivariate Normal

%# Partition the data into two matrices
X1 = X(:, 1:2);
X2 = X(:, 3:4);

%# Find the canonical correlations of the two matrices
[A, B, r] = canoncorr(X1, X2);

The first canonical correlation is the first element of r, and the second canonical correlation is the second element of r.

The canoncorr function also has a lot of other outputs. I'm not sure I'm clever enough to provide a satisfactory yet concise explanation of them here so instead I'm going to be lame and recommend you read up on it in a multivariate analysis textbook - most multivariate analysis textbooks will have a full chapter dedicated to canonical correlations.

Finally, if you don't have the statistics toolbox, then a quick google revealed the following FEX submission that claims to provide canonical correlation analysis - note, I haven't tested it myself.

like image 68
Colin T Bowers Avatar answered Sep 18 '22 17:09

Colin T Bowers


Ok, let's have a short try:

A = [1:20; rand(1,20)]'; % Generate some data...

The best way to examine a 2-dimensional relationship is by looking at the data plots:

plot(A(:,1), A(:,2), 'o') % In the random data you should not see some pattern...

If we really want to compute some correlation coefficients, we can do this with corrcoef, as you mentioned:

B = corrcoef(A)
ans =

    1.0000   -0.1350
   -0.1350    1.0000

Here, B(1,1) is the correlation between column 1 and column 1, B(2,1) between column 1 and column 2 (and vice versa, thus B is symmetric).

One may argue about the usefulness of such a measure in a two-dimensional context - in my opinion you usually gain more insights by looking at the plots.

like image 44
Thilo Avatar answered Sep 20 '22 17:09

Thilo