assembling a correlation matrix

1 次查看(过去 30 天)
Bush Merc
Bush Merc 2013-10-19
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

回答(1 个)

Azzi Abdelmalek
Azzi Abdelmalek 2013-10-19
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 个评论
Azzi Abdelmalek
Azzi Abdelmalek 2013-10-19
编辑:Azzi Abdelmalek 2013-10-19
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 2013-10-19
编辑:Azzi Abdelmalek 2013-10-19
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by