please make my code easier
7 views (last 30 days)
Show older comments
Hi guys,
below I have a code which with the input of K and N and NGTNo1, calculates a formula. it works properly but the problem is that it lasts too long. Any suggestion to make it faster?
G=zeros(5000,5000);
K=1;
for i=1:11293
A=N(i,1:5000);
B=NGTNo1(i,:);
F=zeros(5000,5000);
for n=1:K
C=B(n);
D=N(C,1:5000);
E=((A-D)'*(A-D))/K;
F=F+E;
end
G=F+G;
end
best regards, Aram
1 Comment
Matt Kindig
on 12 Jul 2013
Edited: Matt Kindig
on 12 Jul 2013
While it's not clear the size of NGTNo1, from the code it is clear that N is at least 11293x5000, resulting in 5.6465e7 elements. If these are doubles, this occupies 4.5e8 bytes, or ~431 MB, of memory. This is a rather large matrix for Matlab, and thus you might be running into memory limitations, which would tend to slow down your program.
You might want to examine the size of N and NGTNo1 (using the 'whos' command), and compare it to the memory information given by the memory() function. My guess is that you are nearing the "Maximum possible array" size for your system.
I especially imagine that the line:
E = ((A-D)'*(A-D))/K;
is rather memory-intensive, as there are several temporary matrices that must be created here. You might want to split this calculation into several lines to avoid this. You could also create a new variable H = A-D to use in the calculation of E. I'm actually not sure whether the JIT accelerator is smart enough to recognize that A-D is calculated twice in this line, and do this substitution internally.
Accepted Answer
More Answers (2)
Aram
on 12 Jul 2013
1 Comment
Matt J
on 12 Jul 2013
Edited: Matt J
on 12 Jul 2013
Aram, since you're new to the board I can see that you don't know about Accept-clicking answers. Please accept-click mine (the one with the actual solution in it) and consider also accept-clicking Star Strider's in one of your earlier questions
if it helped you.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!