write equation in for loop

조회 수: 2 (최근 30일)
Ancy S G
Ancy S G 2022년 2월 17일
댓글: Voss 2022년 2월 18일
I have to update this equation
  • Pij^(k+1)=[Pij^k-a^k*(xij^k-yij^k)]+
where i represents row, j represents coloumn and k represents the iteration.
How to write this equation in for loop

채택된 답변

Voss
Voss 2022년 2월 17일
If you want to overwrite the values of a, x, y, and P on each iteration, then maybe you can do this:
P = P-a*(x-y);
If instead you want to store the values of a, x, y, and P for each iteration, then maybe it would be something like this:
P(:,:,k+1) = P(:,:,k)-a(k)*(x(:,:,k)-y(:,:,k));
or perhaps this:
P{k+1} = P{k}-a(k)*(x{k}-y{k});
depending on whether the sizes of P, x, and y might change with each iteration.
  댓글 수: 2
Ancy S G
Ancy S G 2022년 2월 18일
Ok.thank you.
But I have to change the i,j along with each iteration.If a 3*2 matrix is considered,then I want P11^(k+1),P12^(k+1), upto P32^(k+1) and if Pij^(k+1)-Pij^(k)>0.1,then go for next iteration. How it is possible in for loop?
Voss
Voss 2022년 2월 18일
The notation in my answer (the MATLAB syntax) uses all elements of P. In other words, it uses all i,j.
You can apply the condition that Pij^(k+1)-Pij^(k)>0.1 to each individual element of P in a couple of different ways, depending on (Case 1) whether the each element should stop being updated when the condition is no longer met for that element, or (Case 2) whether they all should stop being updated only when the condition is no longer met in all elements.
In either case, the condition can be used to iterate in a while loop like this:
while any(any(P(:,:,k+1)-P(:,:,k) > 0.1))
% do another iteration
end
and then the code within the while loop will update a and x, y either at all elements or only some depending on how you write it.

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

추가 답변 (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