like in box name have s1 s2 s3 s4 s5 s6 .. sn
s1 = 1
s2 = 2
s3 = 3
sn = n
**************************
for i=1:n
s<i> do something
end
************
behind s<> how to code variable to code

답변 (1개)

madhan ravi
madhan ravi 2019년 7월 3일

0 개 추천

댓글 수: 8

Anantachai Chotkamsri
Anantachai Chotkamsri 2019년 7월 3일
편집: Anantachai Chotkamsri 2019년 7월 3일
sry i can't find my answer T^T
Walter Roberson
Walter Roberson 2019년 7월 3일
The answer is "Don't DO That!"
There are ways to do it, but there are very very few good reasons to do it considering the alternatives.
Anantachai Chotkamsri
Anantachai Chotkamsri 2019년 7월 3일
편집: Anantachai Chotkamsri 2019년 7월 3일
pls. can u teach me? cos my algorithm need to use this function
Rik
Rik 2019년 7월 3일
Explain what you want to do. Then we can help you solve your actual problem.
Anantachai Chotkamsri
Anantachai Chotkamsri 2019년 7월 3일
i want to know how to use for loop for varible
example >> i create
-------------------------------------
for i=1:1000
eval(sprintf('s%d = i+1', i));
end
------------------------------------------
i will got s1 ,s2 ,s3 , ...sn on work space.
but it too many n i can't typing. i want to use for loop to help but i dont know about code.
-----------------------------------------------------------------------
for i=1:n
s( in this blacket how to tell matlab when i use for loop )
i know i can typing
s1 = do something
s2 = do something
s3 = do something
but i have a lot of n. i want for loop to do this for me.
like
s(i) = do something <<< for easy than typing
end
-----------------------------------------------------------------------
thank you.
I think you already identified the problem, do not create variables s1,s2,s3 i n the first place.
s=ones(1000,1);
for i=1:1000
s(i)=i+1;
end
Stephen23
Stephen23 2019년 7월 3일
@Anantachai Chotkamsri: what is stopping you from just using indexing?
Indexing is simple, neat, and very effiicient (unlike what you are trying to do).
Indexing is also explained in the introductory tutorials:
and also explained in the link that madhan ravi gave you.
Anantachai Chotkamsri
Anantachai Chotkamsri 2019년 7월 3일
thank you so much for help everyone. <3

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

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

질문:

2019년 7월 3일

댓글:

2019년 7월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by