Borrar filtros
Borrar filtros

How to assign data to dynamic variable

27 visualizaciones (últimos 30 días)
Ole
Ole el 18 de Nov. de 2022
Comentada: Stephen23 el 18 de Nov. de 2022
How to create variables as amany as the length of some vector c and assign data to them
The variables are a1 a2 a3.. a... a10
I would like to assign a1 = [ some data] but would like the script to input the index 1 after a
c = 1:10
for b = 1:length(c)
v = 2*[1:b]
eval(string(['a',num2str(b)])) = v
end

Respuesta aceptada

KSSV
KSSV el 18 de Nov. de 2022
c = 1:10 ;
a = cell(length(c),1) ;
for i = 1:length(c)
a{i} = 2*[1:i] ;
end
celldisp(a)
a{1} = 2 a{2} = 2 4 a{3} = 2 4 6 a{4} = 2 4 6 8 a{5} = 2 4 6 8 10 a{6} = 2 4 6 8 10 12 a{7} = 2 4 6 8 10 12 14 a{8} = 2 4 6 8 10 12 14 16 a{9} = 2 4 6 8 10 12 14 16 18 a{10} = 2 4 6 8 10 12 14 16 18 20
You can access cell a by a{1},a{2}, .....a{10}.
DEfining variables like a1, a2, ...a10 is meaning less and not required.

Más respuestas (0)

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by