index of control loop that is a vector component

1 回表示 (過去 30 日間)
genevois pierre
genevois pierre 2012 年 10 月 4 日
hi all,
in order to make my code more compact, i tried this :
for x(1) = lb(1) : d_x(1) : ub(1)
for x(2) = lb(2) : d_x(2) : ub(2)
... and so on ...
and I got an error coming from left side of = sign, e.g. from x(1) and x(2) : Matlab R2010b seems not to accept such indexes that are array elements, meanwhile they are equivalent to scalars.
Is there another way of doing than using everywhere scalars for indexes, e.g. :
x1 = x(1);
x2 = x(2);
for x1 = lb(1) : d_x(1) : ub(1)
for x2 = lb(2) : d_x(2) : ub(2)
... and so on ...
notice that right hand side of = sign seems OK. Also, for the sake of readability, I don't want to use recursivity.
Thanks to all

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 10 月 4 日
編集済み: Azzi Abdelmalek 2012 年 10 月 4 日
why for x(1) use
for x=
and why x=x(l)? the values lb(1) : d_x(1) : ub(1) will be assigned to x!
  2 件のコメント
genevois pierre
genevois pierre 2012 年 10 月 4 日
Thanks for your answer ; actually I have seven variables : x(1) to x(7) (i.e. those 7 variables are contained in the vector x), to make vary independantly, using FOR loops. In the example, I showed only the two first FOR statements.
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 10 月 4 日
編集済み: Azzi Abdelmalek 2012 年 10 月 4 日
when you use a variable x in the loop, the name you give to the variable is not important.
for x1=1:10
y=sin(x1) %for example
end
is the same then
for x2=1:10
y=sin(x2) %for example
end

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by