Creating a new variable in each iteration...
Ältere Kommentare anzeigen
I want to create a new variable in each iteration of a for loop... x_1, x_2, x_3, etc... up to x_50
count = 0;
for i = 2:2:100
count = count + 1;
x_(...I want "count" to go in here... what is the code to achieve this?...) = i^2
end
Basically, there should be an x_1 = 4 created in the first iteration, x_2 = 16 in the second iteration, etc... up to x_50 = 10000
Thanks
D Howard
Note: I have seen others ask similar questions, with experienced users saying things like "this is a bad idea because it is hard to reference these later..." and they suggest creating a vector, but my problem is more general and this is not an option.
1 Kommentar
Akzeptierte Antwort
Weitere Antworten (1)
Walter Roberson
am 20 Feb. 2012
2 Stimmen
As best I recall at the moment, I have not had a need for dynamic variable names since the days I programmed in REXX ... 25 years ago.
1 Kommentar
Doron
am 20 Feb. 2012
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!