How to keep track of iterate numbers in variable names?

4 次查看(过去 30 天)
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.

采纳的回答

Walter Roberson
Walter Roberson 2012-9-23

更多回答(2 个)

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

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by