Clear Filters
Clear Filters

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

3 views (last 30 days)
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 Comment
the cyclist
the cyclist on 16 May 2023
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.

Sign in to comment.

Answers (1)

Rohit
Rohit on 28 Aug 2023
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

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!