MATLAB Answers

How to Preallocate this loop?

6 views (last 30 days)
%clear all
tic
ncut=60;
alpha=3;
for nloop=1:ncut
zx=(-3+(nloop/10));
xaxis(nloop)=zx;
for ploop=1:ncut
zy=(-3+(ploop/10));
yaxis(ploop)=zy;
z=zx+(1j*zy);
term1=(1/pi*(1+exp(-2*abs(z-alpha)^2)));
term2=(exp(-2*abs(z-alpha)^2));
term3=(exp(-2*abs(z+alpha)^2));
term4=(exp(-2*((abs(z)^2)+z*conj(alpha)-alpha*conj(z))));
term5=(exp(-2*((abs(z)^2)-z*conj(alpha)+alpha*conj(z))));
term6=(term2+term3+term4+term5)*term1;
wig(nloop,ploop)=real(term6);
end
end
surf(xaxis,yaxis,wig)
timelapsed = toc
how to preallocate the for loop?

Accepted Answer

KALYAN ACHARJYA
KALYAN ACHARJYA on 4 Mar 2021
Edited: KALYAN ACHARJYA on 4 Mar 2021
xaxis=zeros(1,ncut);
yaxis=zeros(1,ncut);
wig=zeros(ncut,ncut);
More: Avoid using xaxis and yaxis as variable names, there may be possibility of conflicts with Matlab inbuilt function.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by