필터 지우기
필터 지우기

Delay in for loop

조회 수: 6 (최근 30일)
Joe Ajay
Joe Ajay 2012년 12월 28일
Hi,
This is my code. executing a function and writing the values in an excel file.
After certain iterations 'n' it has to find the average of the values and that is how I planned.
But somewhere it went wrong and when I give 'n' as 2. i.e the no.of iteration for for loop be 2.
Instead of getting the result with in 5 or 7 seconds (when I do not calculate the average) The loop doesn't stop also it doesn't give any error msg.
When I close the Matlab window and open the written file there I get my answer written in the excel file.
What makes the for loop to take so long and what happens during that time.
Is there any way I can get the average of the values it.
Thanks
for i=1:n
tic;
[X,FVAL] = ga(@func1a,4,[],[],[],[],[-2 500 45 200],[2 1800 72 1600],[],optimset('TolX',1e-12));
t=toc;
if (i==1)
y(i,:)=[[],[],X,t];
cel = sprintf('A%i', i);
w = {'GA','Trail.No','A','B','C','D','Time';[],i,X(1),X(2),X(3),X(4),t};
xlswrite('d:\Office.xls',w,1,cel);
else
y(i,:)=[[],[],X,t];
cel = sprintf('A%i', i+1);
a={[],i,X(1),X(2),X(3),X(4),t};
xlswrite('d:\Office.xls',a,1,cel);
while i==n
a=sum(y)/n;
cel = sprintf('A%i', i+2);
xlswrite('d:\Office.xls',a,1,cel);
end
end
end

답변 (1개)

Walter Roberson
Walter Roberson 2012년 12월 28일
In your loop you have
while i==n
a=sum(y)/n;
cel = sprintf('A%i', i+2);
xlswrite('d:\Office.xls',a,1,cel);
end
but inside that loop you do not change "i" and you do not change "n" so if the loop condition is ever true it will remain true and you infinite loop.

카테고리

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