assembling a correlation matrix
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi,
I am creating a correlation matrix (H) using the following routine. However for large values of M,N it takes a long time to create H. I was wondering if there other ways of creating H more efficiently.
X=15;
%
N=10;M=20;
K=randi(X,M,N);
L=randi(X,M,N);
H=zeros(X,X);
for k=1:X
for l=1:X
H(k,l)=hsum(k,l,K,L,M,N);
end
end
%where hsum is the following function
function ret=hsum(x,y,C,G,M,N)
ret=0;
for i=1:M
for j=1:N
if(C(i,j)==x && G(i,j)==y)
ret= ret+1;
end
end
end
Thanks Mark
0 Kommentare
Antworten (1)
Azzi Abdelmalek
am 19 Okt. 2013
You can change your function
function ret=hsum(x,y,C,G,M,N)
a=C(1:M,1:N);
b=G(1:M,1:N);
ret=sum(find(C==x and G==y));
2 Kommentare
Azzi Abdelmalek
am 19 Okt. 2013
Bearbeitet: Azzi Abdelmalek
am 19 Okt. 2013
Bush Merc commented
Thanks for the suggestion. Unfortunately it is slower than the "for" loop.
function ret=hsum(x,y,C,G,M,N)
ret=sum(logical(find(C==x & G==y)));
Azzi Abdelmalek
am 19 Okt. 2013
Bearbeitet: Azzi Abdelmalek
am 19 Okt. 2013
try
unction ret=hsum(x,y,C,G,M,N)
ret=sum(C==x & G==y);
%If you want to add a comment, click on comment on this answer
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!