Computing Spearman coefficient between corresponding rows of two matrices in computationally efficient way

조회 수: 16 (최근 30일)
I have multiple pairs of large matrices (nx1000) stored inside cell array. I have devised a way to extract the values and then use cellfun to calculate the spearman correlation coefficient. However, I end up with a nxn matrix, which takes a lot of time to compute. I only want the diagonal values of the nxn matrix, and only want to compute the coefficient for corresponding rows of the two matrices, instead of computing it for all pairs of rows. How shall I do this? The ("Rows", "pairwise") option in the corr() function does not seem to help.
  댓글 수: 1
the cyclist
the cyclist 2023년 5월 16일
Can you upload the data? (You can use the paper clip icon in the INSERT section of the toolbar.) Or maybe a small subset? Uploading the code you use to get what you have now would also be helpful.
I'm finding it a little difficult to follow your explanation, but seeing your code would presumably explain it all.

댓글을 달려면 로그인하십시오.

답변 (1개)

Rohit
Rohit 2023년 8월 28일
Hi Alekhya,
I understand that you want to calculate the Spearman correlation coefficient only for corresponding rows of large matrices stored inside a cell array and obtain only the diagonal values.
To do this you can iterate over each row index, extract the corresponding rows from the matrices, and compute the Spearman correlation coefficient using the corr function.
Here is an example of how you can compute the correlation:
% Sample input data
matrices = {rand(5, 1000), rand(5, 1000)}; % Example cell array with two matrices
% Initialize a vector to store the diagonal correlation coefficients
diagonal_correlations = zeros(size(matrices{1}, 1), 1);
% Iterate over each row index
for i = 1:size(matrices{1}, 1)
% Extract the rows from the matrices
row1 = matrices{1}(i, :);
row2 = matrices{2}(i, :);
% Compute the Spearman correlation coefficient
correlation = corr(row1', row2', 'Type', 'Spearman');
% Store the correlation coefficient in the diagonal_correlations vector
diagonal_correlations(i) = correlation
end

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by