Computing Spearman coefficient between corresponding rows of two matrices in computationally efficient way
14 ビュー (過去 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!