please make my code easier

조회 수: 7 (최근 30일)
Aram
Aram 2013년 7월 12일
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
Matt Kindig
Matt Kindig 2013년 7월 12일
편집: Matt Kindig 2013년 7월 12일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2013년 7월 12일
편집: Matt J 2013년 7월 12일
K=1;
N=N(:,1:5000).';
NGTNo1=NGTNo1(:,1:K);
G=0;
for n=1:K
A=N;
C=NGTNo1(:,n);
D=N(:,C);
AmD=(A-D);
G=G+AmD*AmD.';
end
G=G/K;

추가 답변 (2개)

Aram
Aram 2013년 7월 12일
Thanks Matt but there is an error in D=N(C,:). also I didn't realized why you wrote sqrt(K). It should it should simply divided by K.
regards, Aram
  댓글 수: 1
Matt J
Matt J 2013년 7월 12일
편집: Matt J 2013년 7월 12일
Try again, Aram. I've edited the code. The sqrt(K) is squared implicitly in the expression AmD*AmD.', but I've changed that part anyway.

댓글을 달려면 로그인하십시오.


Aram
Aram 2013년 7월 12일
That's extremely safe and fast. thanks Matt :)
  댓글 수: 1
Matt J
Matt J 2013년 7월 12일
편집: Matt J 2013년 7월 12일
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.

댓글을 달려면 로그인하십시오.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by