필터 지우기
필터 지우기

How can i speed up this part of my code?

조회 수: 1 (최근 30일)
amanita
amanita 2013년 4월 18일
I have this part of code that i apply for different indns (which are different vectors each time):
phi1=phi(indn1);
p1=p(indn1,indn1);
rtn1=(phi1'*(p1*phi1));
Ctn1=((rtn1*ep2)/(st*(1+rtn1)));
Ctn1=max(Ctn1,0);
Sn1=gammainc(Ctn1*0.5,0.5) ;
Sn1=1-Sn1;
phi2=phi(indn2);
....
....
I tried cellfun and structfun but it was slower. Is there something more i can do to improve the speed?
  댓글 수: 5
amanita
amanita 2013년 4월 19일
thanx Cedric
amanita
amanita 2013년 6월 16일
The problem was solved by creating mex files and calling them through the GA btw.

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

채택된 답변

Roger Stafford
Roger Stafford 2013년 6월 17일
In your code the multiplication part of doing
phi1'*p1*phi1
is being repeated over and over again as you select different index combinations. Perhaps you could speed up things, even in your mex method, by doing things in the following way. (I am assuming phi is a column vector: one column, with the same number of rows as there are rows and columns in p.)
First, compute, one time only, the matrix
A = (phi*phi').*p;
Then for each new indn vector as it comes along do only this
rtn = sum(sum(A(indn,indn)));
or
rtn = sum(reshape(A(indn,indn),[],1));
whichever is faster. This step requires only addition operations rather than the multiplications plus additions required for matrix multiplication, and gives the same result.
Also in gammainc use the 'upper' option instead of doing Sn1=1-Sn1.
  댓글 수: 1
amanita
amanita 2013년 6월 17일
Thanx Roger! This really helped.
Sum sum was faster than reshape, the 'upper' though i think makes it a little bit slower.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by