How to Preallocate this loop?

1 次查看(过去 30 天)
%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?

采纳的回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2021-3-4
编辑:KALYAN ACHARJYA 2021-3-4
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by