Effacer les filtres
Effacer les filtres

create arrays in a loop

1 vue (au cours des 30 derniers jours)
Rune
Rune le 31 Juil 2013
is it possible to create arrays in a loop. For instance iterate over a i=1:10
and for each value of i create a new array (with a specific name with a connection to the value of i)
later on the array can be filled in.
  1 commentaire
Andrew Reibold
Andrew Reibold le 31 Juil 2013
This is very possible. Have an example?

Connectez-vous pour commenter.

Réponses (2)

Azzi Abdelmalek
Azzi Abdelmalek le 31 Juil 2013
This is not a good idea, why create multiple variables while you can use just one cell array. Look at http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F

Iain
Iain le 31 Juil 2013
This is how to do it:
for i = 1:10
eval(['variable_' num2str(i) ' = zeros(5);']);
end
Azzi is correct, it is usually a bad idea to programmatically create variable names as it means that you will then struggle to use all of those variables, like for example, when you want to calculate the sum of them all.
If you can avoid having one cell array to store all your data, and use a well formatted 2-D, 3-D or n-D array instead, you probably should.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by