How do I compute the correlation between corresponding rows of two matrices?

55 ビュー (過去 30 日間)
Michael Wolf
Michael Wolf 2018 年 5 月 11 日
コメント済み: Michael Wolf 2018 年 5 月 11 日
I have two matrices, X and Y, which each have 60 columns and 10,000 rows.
I would like to create a vector R, such that the nth value of vector R represents the correlation between row n of matrix X and row n of matrix Y.
I have tried:
[R,pval] = corr(X',Y');
but this gives me R as a 10,000-by-10,000 matrix.
What am I doing wrong?

採用された回答

possibility
possibility 2018 年 5 月 11 日
X is 10,000-by-60 Y is 10,000-by-60
X' is 60-by-10,000 Y' is 60-by-10,000
By correlating these two matrices, i.e. R=corr(X', Y'), you get the 10,000-by-10,000 R matrix whose elements are the pairwise correlation of each column of X' and Y', that is each row of X and Y.
So, you are doing the right thing! Just check the diagonals of that matrix:
>> Rvec= diag (R)
  1 件のコメント
Michael Wolf
Michael Wolf 2018 年 5 月 11 日
diag (R) gave me a vector of 10000 elements that seem to represent correlations for each row of the input matrices.
Here is an added challenge: I attempted to apply this technique to a larger pair of matrices (also 60 rows, but ~60000 columns instead of 10000)--and the code would not run because the R matrix (around 60000-by-60000 in size) exceeded Matlab's size limit. Is there a way around this?

サインインしてコメントする。

その他の回答 (2 件)

Von Duesenberg
Von Duesenberg 2018 年 5 月 11 日
arrayfun(@(k) corr(A(k,:)', B(k,:)'), 1:10000, 'Uni', 1)

Shounak Shastri
Shounak Shastri 2018 年 5 月 11 日
From what I know, Correlation Matrices are usually square. As in you have 10000 rows with 60 elements in each row. So if you take 1 row of dimension 1 x 60, the dimension of your corr matrix would be 60 x 60.
But in your example, you took a transpose. So, now the size of each row is 1 x 10000. And thus when you perform correlation on the rows, you would get a 10000 x 10000 matrix.
  1 件のコメント
Michael Wolf
Michael Wolf 2018 年 5 月 11 日
Thanks! I was looking for the correlation coefficient between each pair of rows. Apparently, taking the diagonal of this correlation matrix gives me that.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by