If I am have this code as example
while(A>0.0000001);
statment
.
.
.
end
How can save value of A to each iteration until reach 0.0000001?
I mean there is (A) at first iteration, (A) at second iteration,and so on

 채택된 답변

Walter Roberson
Walter Roberson 2013년 12월 4일

1 개 추천

K = 1;
A(K) = .... some initial value ...
while( A(K) >0.0000001)
statment
.
.
.
K = K + 1;
A(K) = new value for A
end

댓글 수: 3

Mary Jon
Mary Jon 2013년 12월 4일
How can I know the time taken by A to reach convergence in seconds
doc tic % toc
Mary Jon
Mary Jon 2013년 12월 4일
Thank you dpb

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Jeremy Wurbs
Jeremy Wurbs 2013년 12월 4일

1 개 추천

If you know how long you're going to iterate for, something like:
numIter = 10;
A_vec = zeros(numIter,1);
A = 10; cnt = 1;
while(A>0)
A_vec(cnt) = A;
A = A-1;
cnt = cnt+1;
end
If you don't know how long you will iterate for you might not be able to preallocate efficiently (which will greatly slow things down). That said, it is legal to do the following:
A_vec = [];
A = 10;
while(A>0)
A_vec(end+1) = A;
A = A-1;
end
dpb
dpb 2013년 12월 4일

1 개 추천

A=[];
iter=0;
while...
iter=iter+1;
A(i)=...;
...
end
NB that if iter grows very large the reallocation every iteration above may begin to noticeably slow down the execution. To minimize this, start by preallocating A and filling. You'll have to check for overflow and reallocate if needed or make the initial size large enough to never be exceeded in which case you can then truncate when done.
N=100;
A=zeros(N,1);
iter=0;
while...
if iter==N, A=[A; zeros(1,N)]; N=N+N; end % add another chunk
iter=iter+1;
A(i)=...;
...
end

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

태그

질문:

2013년 12월 4일

댓글:

2013년 12월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by