assembling a correlation matrix
1 view (last 30 days)
Show older comments
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 Comments
Answers (1)
Azzi Abdelmalek
on 19 Oct 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 Comments
Azzi Abdelmalek
on 19 Oct 2013
Edited: Azzi Abdelmalek
on 19 Oct 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
on 19 Oct 2013
Edited: Azzi Abdelmalek
on 19 Oct 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
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!