t=0:0.01:100;
l=[0.1:0.5:100];
b=bsxfun(@(a,b)exp(-a.*b),t,l.')
For the simple case using a row and a column vector, bsxfun evaluates all combinations in a matrix.
Update:
It is possible to increase the speed by a factor of about 4 using bsxfun
with times
. This is faster because there is a highly optimized implementation for bsxfun
for some basic math operations.
t=0:0.01:100;
l=[0.1:0.5:100];
b=exp(bsxfun(@times,-t,l.'))
Third solution, very simple and nearly as fast as my improved or natas solution. Simply preallocate a
t=0:0.01:100;
f=@(t,l) exp(-t.*l)
l=[0.1:0.5:100];
a=nan(numel(l),numel(t));
for ll=1:length(l)
a(ll,:)=f(t,l(ll));
end
Matlab should have marked the line a(ll,:)=f(t,l(ll));
with a yellow warning in the editor, it's worth reading them. If you click on it, preallocating is explained.