Saving successive results ?

1 次查看(过去 30 天)
Jason
Jason 2011-8-8
Hi. I need some help with the problem below.
for k = 1:1:n;
sum = k^2+2k; % Just an example
end
At the end of the run, I wish to have a total of n variables named, say, array_1, array_2, ... array_n. Each array should contain the value of the sum associated with the respective k. So, array_1 = 3, array 2 = 8, and so on.
I am having trouble renaming the variable. Any help?

采纳的回答

Friedrich
Friedrich 2011-8-8
Hi,
what you like to do can be done with eval:
n = 5;
for k = 1:1:n;
eval(['array_',num2str(k),'= k^2+2*k;']); % Just an example
end
But I would recommend avoid eval and use a Cell instead to capture the output:
n = 5;
array = cell(n,1);
for k = 1:1:n;
array{k} = k^2+2*k;
end
For some more details look under the following link for "How can I create variables A1, A2,...,A10 in a loop?"
  3 个评论
Jason
Jason 2011-8-8
Very well, thanks a lot for the cell suggestion.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by