How to keep track of iterate numbers in variable names?

4 views (last 30 days)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 23 Sep 2012

More Answers (2)

Azzi Abdelmalek
Azzi Abdelmalek on 23 Sep 2012
Edited: Azzi Abdelmalek on 23 Sep 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 on 23 Sep 2012
Edited: Rick Rosson on 23 Sep 2012
N = 4;
product = zeros(N,1);
for k = 1:N
product(k) = 3*k;
end

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!