How to Preallocate this loop?

조회 수: 3 (최근 30일)
suriyaprasanth s
suriyaprasanth s 2021년 3월 4일
편집: KALYAN ACHARJYA 2021년 3월 4일
%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