How to speed up this code? Or vectorize?
5 views (last 30 days)
Show older comments
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 Comment
Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!