How to speed up this code? Or vectorize?

조회 수: 4 (최근 30일)
Philipp Ochsendorf
Philipp Ochsendorf 2019년 1월 31일
편집: Jan 2019년 2월 1일
hi, i got the following code. Is there a way to completly vectorize it ? or rather a better way of programming this for faster execution in e.g fmincon.
c=positive_int;
% create alpha for case n=0;
j=0:50;
alpha(0+1,j+1)= gamma((c.*j)+1)./gamma(j+1);
cache=zeros(51,1);
for n=1:1:10
for j=n:1:50
% make array of new_alpha(j) in a matrix with old_alpha in column n
% with increasing index the the new_alpha, new_alpha is a sum of old_alpha from old column with index * function(value_index)
% e.g new_alpha(1)=old_alpha(1)*function(with value1);
% new_alpha(2)=old_alpha(1)*function(with value1)+ old_alpha(2)*function(with value2); ....
% and that for every n
for m=n-1:1:j-1
% recursive using alpha:
cache(m+1)= alpha(n-1+1,m+1)*gamma((c*j)-(c*m)+1)/gamma(j-m+1);
end
alpha(n+1,j+1)=sum(cache);
cache(:)=0;
end
end
So far it got this.
c=positive_int;
j=0:50;
alpha(j+1,0+1)= gamma((c.*j)+1)./gamma(j+1);
variable=50;
[mValues, nValues] = meshgrid(0:variable, 0:variable);
mask = nValues <= mValues;
for n=1:1:10
% Operation for one new alpha_array:
[j,m]=meshgrid(n:1:50 ,n-1:1:49);
cache= alpha(m(1:50-n+1)+1,n-1+1).*gamma((c.*j)-(c.*m)+1)./gamma(j-m+1);
mask(51-n,:)=[];
mask(:,51-n)=[];
cache=cache.*mask;
cache(isnan(cache))=0;
cache=sum(cache);
alpha(n+1:end,n+1)=cache;
end
But that seems a bit complex coded and is not that huge improvement.
  댓글 수: 1
Jan
Jan 2019년 2월 1일
편집: Jan 2019년 2월 1일
This let your array shrink iteratively, what is a don't for efficient programming:
mask(51-n,:)=[];
mask(:,51-n)=[];
This happens 10 times only, so it will not matter much.
Please do not let the readers guess, what "positive_int" is. Post running code.

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by