フィルターのクリア

'For' loop for specified integers (more than one)

3 ビュー (過去 30 日間)
Eirini Gk
Eirini Gk 2016 年 5 月 10 日
コメント済み: Stephen23 2016 年 5 月 10 日
Hello, I know that we can use for loop for specified integers like : for v=[1 5 23 99]. But I want to have more than one variable. I have to calculate every iteration in the loop with given initial condition (x,y,a). For example for:[ (x=1,y=0,a=0.2),(x=2,y=1,a=2) ] etc. Can I write it somehow ?
  1 件のコメント
Adam
Adam 2016 年 5 月 10 日
Just put the other variables inside the loop as e.g.
a = a + 1;

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

回答 (2 件)

KSSV
KSSV 2016 年 5 月 10 日
P = [1 0 0.2 ; 2 1 2 ] ;
for i = 1:size(P,1)
x = P(i,1) ; y = P(i,2) ; a = P(i,3) ;
end
  1 件のコメント
Stephen23
Stephen23 2016 年 5 月 10 日
編集済み: Stephen23 2016 年 5 月 10 日
Note that you should not use i for variable names, as this is the name of the inbuilt imaginary unit:
Ditto for j, size, length, etc, etc.
You can use which to check if variable names are already allocated.

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


Stephen23
Stephen23 2016 年 5 月 10 日
編集済み: Stephen23 2016 年 5 月 10 日
The simplest solution is to use indexing:
X = [1,2,3]
Y = [0,1,2];
A = [0.2,2,20];
for k = 1:numel(A)
x = X(k)
y = Y(k)
a = A(k)
end
  5 件のコメント
Eirini Gk
Eirini Gk 2016 年 5 月 10 日
One quastion more because Im quite new user of Matlab. If I have a variable s=0 s=s+1 that gets bigger in every iteration if I 'print' a(s) would it appear a(1), a(2) etc. or not ? Thank u very much
Stephen23
Stephen23 2016 年 5 月 10 日
@Eirini Gk: Yes, it is just basic indexing. You just have to keep in mind that MATLAB indexing (like is common in mathematics) starts from one, not zero.

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by