assembling a correlation matrix

1 visualización (últimos 30 días)
Bush Merc
Bush Merc el 19 de Oct. de 2013
Comentada: Azzi Abdelmalek el 19 de Oct. de 2013
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

Respuestas (1)

Azzi Abdelmalek
Azzi Abdelmalek el 19 de Oct. de 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 comentarios
Azzi Abdelmalek
Azzi Abdelmalek el 19 de Oct. de 2013
Editada: Azzi Abdelmalek el 19 de Oct. de 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
Azzi Abdelmalek el 19 de Oct. de 2013
Editada: Azzi Abdelmalek el 19 de Oct. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by