Azzera filtri
Azzera filtri

creating matrices in a loop

1 visualizzazione (ultimi 30 giorni)
vidyadhar k
vidyadhar k il 20 Lug 2017
Commentato: Stephen23 il 20 Lug 2017
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 Commento
Stephen23
Stephen23 il 20 Lug 2017
"Can anyone help in this regard"
Yes, use indexing. Indexing is much better.

Accedi per commentare.

Risposte (1)

Stephen23
Stephen23 il 20 Lug 2017
Modificato: Stephen23 il 20 Lug 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,

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by