필터 지우기
필터 지우기

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
Friedrich
Friedrich 2011년 8월 8일
Yes it does.
Jason
Jason 2011년 8월 8일
Very well, thanks a lot for the cell suggestion.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by