Computing Spearman coefficient between corresponding rows of two matrices in computationally efficient way
3 次查看(过去 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
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
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!