How to reset a variable in a for loop?

Hello evreyone,
I was wondering if there is a way to reset variables in a for loop, I am going to show you an example to be as clear as possible,
for i=1:10
X=1+rand()
Y=["A","B","C"];
end
What I want to achieve is to clean up the X variable, that is, every time the for loop starts over, the X variable is cleared and recreated. I know it is very expensive for the pc, but all the values obtained in one click are stored in another variable and I need to recreate X. (I know well that in the example I have proposed the values are overwritten, but in the code that I am writing at each loop the values in the variable increase and are not overwritten)

7 件のコメント

Jan
Jan 2021 年 9 月 21 日
In you example X and Y are reset already. So I do not know, what you are asking for.
Andrea Miceli
Andrea Miceli 2021 年 9 月 21 日
I want that at the end of the loop X doesn't exixst anymore and in the new loop the code will build a new X. Is that possible?
Mathieu NOE
Mathieu NOE 2021 年 9 月 21 日
you can add
clear X
in the code where you want the variable being erased
Andrea Miceli
Andrea Miceli 2021 年 9 月 21 日
Hi, Mathieu I have alreay tryed with "clear variable", but it doesn't fit very well even because I got 20-30 variables so I should write all of them down and It would take too long.
It should be like evrey loop is independet so I can compare the results of each loop.
Again, to be as clear as possible, as if every time it repeats the loop, the code is executed for the first time and does not take into account the calculations made earlier.
Stephen23
Stephen23 2021 年 9 月 21 日
@Andrea Miceli: it sounds like most of what you describe would be fixed by writing and calling a function.
Andrea Miceli
Andrea Miceli 2021 年 9 月 22 日
I think you're right, I will try to make a function and use a for loop on the function. Thank you Stephen, It wasn't easy for me explaining what I want to do, but I think this is the best way to get what I want!
Rik
Rik 2021 年 9 月 22 日
Then it seems something like I suggested in your duplicate question should do what you want. Simply create your variables once and then pass them as parameters to your function. Using a struct can help to limit the number of input parameters so you don't get confused by the order (and so you can easily add new ones).

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

回答 (2 件)

Adam Danz
Adam Danz 2021 年 9 月 21 日
編集済み: Adam Danz 2021 年 9 月 21 日

0 投票

The example isn't very clear but I think you want to preserve the random X values within the loop. If so,
n = 10; % number of iterations
x = nan(1,n);
for i = 1:n
x(i) = 1+rand();
...
end
Of course in this example, you could generate all x values at once using x = 1+rand(1,n);
David Hill
David Hill 2021 年 9 月 21 日

0 投票

Use indexing.
X=1+rand(1,10);
for i=1:10
Y=X(i)+5;%whatever you do.
end

カテゴリ

質問済み:

2021 年 9 月 21 日

コメント済み:

Rik
2021 年 9 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by