Borrar filtros
Borrar filtros

How to keep track of iterate numbers in variable names?

1 visualización (últimos 30 días)
How to create a unique variable for the outputs of a for loop?
So for example:
for k=1:4
product=k*3
end
I want to be able to distinguish the iterates and have a result like:
product1=3
product2=6
product3=9
product4=12
This is just a simple example, I need it for a much larger problem.

Respuesta aceptada

Walter Roberson
Walter Roberson el 23 de Sept. de 2012

Más respuestas (2)

Azzi Abdelmalek
Azzi Abdelmalek el 23 de Sept. de 2012
Editada: Azzi Abdelmalek el 23 de Sept. de 2012
why not
for k=1:4
product(k)=k*3
end
or
for k=1:4
product.(sprintf('n%d',k))=k*3
end

Rick Rosson
Rick Rosson el 23 de Sept. de 2012
Editada: Rick Rosson el 23 de Sept. de 2012
N = 4;
product = zeros(N,1);
for k = 1:N
product(k) = 3*k;
end

Categorías

Más información sobre Loops and Conditional Statements 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