Filter löschen
Filter löschen

Compute correlations in 3D arrays

4 Ansichten (letzte 30 Tage)
julian gaviria
julian gaviria am 6 Nov. 2023
Kommentiert: julian gaviria am 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)

Akzeptierte Antwort

Dyuman Joshi
Dyuman Joshi am 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]
ans = 10×2
0.0029 0.4429 0.0032 0.3950 0.0069 0.0684 -0.0013 0.7218 -0.0056 0.1426 -0.0027 0.4775 -0.0026 0.4976 -0.0055 0.1459 -0.0012 0.7442 0.0031 0.4171
%Lower triangle values
[rl pl]
ans = 10×2
0.0007 0.8508 -0.0015 0.6969 -0.0113 0.0028 0.0065 0.0878 0.0001 0.9798 0.0064 0.0892 -0.0038 0.3129 -0.0029 0.4408 -0.0012 0.7495 0.0043 0.2520
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

Weitere Antworten (0)

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by