creating matrices in a loop
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am trying to create matrices in a for loop as below: for a=1:5 u_a=[]; end; My intention is to get u_1,u_2,...,u-5. However, it is creating u_a for five times. Can anyone help in this regard. Thanks in advance!
1 commentaire
Stephen23
le 20 Juil 2017
"Can anyone help in this regard"
Yes, use indexing. Indexing is much better.
Réponses (1)
Stephen23
le 20 Juil 2017
Modifié(e) : Stephen23
le 20 Juil 2017
"My intention is to get u_1,u_2,...,u-5. ... Can anyone help in this regard"
Do not do this!
While it is possible to magically create variable like that, doing so will make your code slow, buggy, hard to read, hard to debug, inefficient, a security risk, etc,. Here is what the MATLAB documentation says about magically creating variable names like that: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array."
As you can read, the much better alternative is use one array and indexing. Indexing is simple, very efficient, neat, easy to read, easy to debug, etc, etc.
You can read more explanations and discussions here:
etc, etc,
0 commentaires
Voir également
Catégories
En savoir plus sur Matrix Indexing 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!