Compute correlations in 3D arrays
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
julian gaviria
le 6 Nov 2023
Commenté : julian gaviria
le 7 Nov 2023
%Random matrices
A=randi(100,374,374);
A_ = eye(size(A,[1 2]));
A(ones(size(A))&A_)=NaN;
B=randi(100,374,374);
B_ = eye(size(B,[1 2]));
B(ones(size(B))&B_)=NaN;
The following code computes correlation coeficient and p value from matrices A, B:
nn=374;
temp= ~eye (nn);
ii_all_conn = find(temp>0);
ii_uptri_conn = find(triu(temp,1)> 0);
ii_lotri_conn = find(tril(temp,-1)> 0);
%Corr plots up entries
figure, plot(A(ii_uptri_conn), B(ii_uptri_conn),'o');
[r,p]= corr(A(ii_uptri_conn), B(ii_uptri_conn));
title(['Upper connections - r = ' num2str(r) ' (p ' num2str(p) ')']);
%Corr plots low entries
figure, plot(A(ii_lotri_conn), B(ii_lotri_conn),'o');
[r,p]= corr(A(ii_lotri_conn), B(ii_lotri_conn));
title(['Lower connections - r = ' num2str(r) ' (p ' num2str(p) ')']);
Can I compute the same correlation and p-value in multidimensional arrays? E.g.
A_3D=randi(100,374,374,10);
B_3D=randi(100,374,374,10);
In the output, the first r and p values would correpond to the Pearson coeficient of A(:,:,1), B(:,:,1). and the tenth r and p values correpond to the Pearson coeficient of A(:,:,10), B(:,:,10)
0 commentaires
Réponse acceptée
Dyuman Joshi
le 7 Nov 2023
Run a for loop through the 3rd dimension -
A_3D = randi(100,374,374,10);
B_3D = randi(100,374,374,10);
s = size(A_3D,3);
[ru, pu, rl, pl] = deal(zeros(s,1));
for k = 1:s
[ru(k), pu(k), rl(k), pl(k)] = correlation(A_3D(:,:,k), B_3D(:,:,k));
end
%Upper triangle values
[ru pu]
%Lower triangle values
[rl pl]
function [Ru, Pu, Rl, Pl] = correlation(A, B)
A = modify(A);
B = modify(B);
temp= ~eye(size(A,[1 2]));
%% Logical indexing is faster than find()
ii_uptri_conn = triu(temp,1)> 0;
ii_lotri_conn = tril(temp,-1)> 0;
[Ru,Pu] = corr(A(ii_uptri_conn), B(ii_uptri_conn));
[Rl,Pl] = corr(A(ii_lotri_conn), B(ii_lotri_conn));
end
function in = modify(in)
temp = eye(size(in,[1 2]));
in(ones(size(in))&temp) = NaN;
end
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!