Save loop variable naming problem

1 visualización (últimos 30 días)
peter huang
peter huang el 30 de Ag. de 2022
Comentada: Stephen23 el 31 de Ag. de 2022
I want to save the answers in the loop for future use, I understand that I can use sprintf to batch store the data in the variables, but if I want to change my name with the loop, how can I do it?
ex.
for i = 1:10
k(i) = 2^i
end
k =
2 4 8 16 32 64 128 256 512 1024
I want to name the variables separately as
k1 = 2
k2 = 4
k3 = 8
....
k10 = 1024

Respuestas (1)

Image Analyst
Image Analyst el 30 de Ag. de 2022
Please don't do that. It's a bad idea. Why not do it? See the FAQ:
  6 comentarios
peter huang
peter huang el 31 de Ag. de 2022
Great, I think this method is much better than eval Then I would like to ask how the variable I want to access today is not 1*n, but may have 68*n and then 20 years of data every year.

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Identification en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by