How can I store value of a variable in every iteration?

4 ビュー (過去 30 日間)
vedant sonar
vedant sonar 2014 年 12 月 5 日
編集済み: Azzi Abdelmalek 2014 年 12 月 6 日
I want to update the value of a variable in every iteration, say in a for loop. I was trying this by specifying the iteration variable as an argument to the concerned variable, like A(i,itr) and A(i,itr+1), where itr=iteration variable and i=loop variable. But, this changes dimension of matrix A. I want values to be updated. Please help.
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 12 月 5 日
Your question is not clear, post an example then explain your problem
vedant sonar
vedant sonar 2014 年 12 月 6 日
編集済み: Azzi Abdelmalek 2014 年 12 月 6 日
Say, I have to update my variable, A in the below for loop in every iteration using its value in a previous iteration,
iteration=1;
for i=1:n
A(i,iteration+1)=A(i,iteration)+ f(A);
end
f(A) is any function of A for updation. This gives incorrect output as matrix A dimensions get changed every time iteration value increments. Please suggest solution.

サインインしてコメントする。

回答 (1 件)

Devin Mair
Devin Mair 2014 年 12 月 5 日
As stated by Azzi, the question is not entirely clear, and it makes it hard to help you without an example.
However, based on what I understand from the question, you want to update the value of your A variable on every iteration of a for loop. Since you didn't give an example, I will show you how to do this using an example of my own creation.
A = 1:10;
% My variable has values of 1 through ten, this is the variable whose values I will update in the %for loop
for step = 1:length(A)
%my for loop will go from 1 through the length of my variable
A(step) = A(step) * 5;
%This is will change the value of my A variable on each iteration to the original value of the variable multiplied by five
end
Hopefully this answers your question. If you update it with an example I'll be able to give you a better answer.
  1 件のコメント
vedant sonar
vedant sonar 2014 年 12 月 6 日
Thanks Devin, this was helpful.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by