"Norman F." wrote in message <jf8p66$11t$1@newscl01ah.mathworks.com>...
> % Variables
> x=0:0.001:1;
> tau=0.1:0.01:0.3;
> b=rand(length(tau),1);
> n=length(x);
> m=length(tau);
>
> A = ones(1,m);
> A(1)=0.5; A(m)=0.5;
> A = A*x(2);
>
> % preallocate iComp
> iComp=zeros(n,1);
>
> % computation
> for l = 1:n
> integrand = b.*exp(x(l)./tau');
> iComp(l) = A * integrand;
> end
>
> Is there some way to avoid the for loop? I tryied arrayfun but it was twice as slow as the for loop...
        
There's no need to multiply by A each of those thousand times. Instead, make the appropriate adjustment in b ahead of time.
% Variables (as before)
x=0:0.001:1;
tau=0.1:0.01:0.3;
b=rand(length(tau),1);
n=length(x);
m=length(tau);
% An alternative
b2 = b/1000; b2([1,m]) = b2([1,m])/2;
iComp = exp((x).'*(1./tau))*b2;
I doubt that this will prove to be very much faster though, because the main computational cost is probably the taking of the exponential function some 20000odd times. However you can try it and see.
Roger Stafford
