Simple Question ( for loop with differnt variables)

2 次查看(过去 30 天)
Heyho folks,
This is a really simple example for the Question I have.
for i=1:3
eval ( ['A' num2str(i) '=i'] )
end
generates A1 = 1 / A2 = 2 /A3 = 3
now i want to use these variables.....but how?
for i= 1:3
B = A{i} + 1
end %nope doesnt work
for i =1:3
B = ([A num2str(i)]) +1
end %nope doesnt work
............

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2014-7-15
编辑:Azzi Abdelmalek 2014-7-15
Why are you complicating things?
for i=1:3
A(i)=i
end
for i= 1:3
B(i) = A(i) + 1
end
  4 个评论
Yoav Livneh
Yoav Livneh 2014-7-15
If you want to use A1, A2, etc. you will have to continue using eval. I recommend switching to an array or cell array, or even a structure, in order to better access your data.
Max Müller
Max Müller 2014-7-15
Thanks, Guys...it worked....guess i am going to sleep now.....

请先登录,再进行评论。

更多回答(0 个)

类别

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