Question

I am searching for a way to get rid of the following loop (over theta):

for i=1:1:length(theta)

  V2_ = kV2*cos(theta(i)); 
  X = X0+V2_;
  Y = Y0-V2_*(k1-k2);
  Z = sqrt(X.^2-Z0-4*V2_.*(k.^2*D1+k1));


  pktheta(:,i)=exp(-t/2*V2_).*(cosh(t/2*Z)+...
            Y./((k1+k2)*Z).*sinh(t/2*Z));  


end

where X0,Y0,Z0 and kV2 are dependent on the vector k (same size). t, D1, k1 and k2 are numbers. Since I have to go through this loop several times, how can I speed it up? Thanks

No correct solution

OTHER TIPS

Try this -

N = numel(theta);

V2_ = kV2*cos(theta(1:N));
X0 = repmat(X0,[1 N]);
Y0 = repmat(Y0,[1 N]);
Z0 = repmat(Z0,[1 N]);

X = X0 + V2_;
Y = Y0-V2_*(k1-k2);
Z = sqrt(X.^2-Z0-4.*V2_ .* repmat(((1:N).^2)*D1 + k1.*ones(1,N),[size(X0,1) 1]));
pktheta = exp(-t/2*V2_).*(cosh(t/2*Z) + Y./((k1+k2)*Z).*sinh(t/2*Z));

Definitely BSXFUN must be faster, if someone could post with it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top