assembling a correlation matrix
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (1)
Azzi Abdelmalek
il 19 Ott 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 Commenti
Azzi Abdelmalek
il 19 Ott 2013
Modificato: Azzi Abdelmalek
il 19 Ott 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
il 19 Ott 2013
Modificato: Azzi Abdelmalek
il 19 Ott 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
Vedere anche
Categorie
Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!