How can I write "n" "for loops" just by a single command?

4 ビュー (過去 30 日間)
Ehsan
Ehsan 2013 年 12 月 12 日
コメント済み: Walter Roberson 2020 年 10 月 30 日
I am interested in writing "n" "for loops" with variables "Var(i)" all varying from 1 to k, just by a single command. In other words I want to make the code automatic instead of writing n for loops manually (please see below). I appreciate your help.
for Var(1) = 1:k
for Var (2) = 1:k
.
.
.
for Var(n) = 1:k
y = f(Var(1),Var(2),...,Var(n))

回答 (1 件)

Walter Roberson
Walter Roberson 2013 年 12 月 12 日
編集済み: Walter Roberson 2013 年 12 月 12 日
Also you should consider using ndgrid and making your function vectorized.
Var = cell(n, 1);
[Var{1:n}] = ndgrid(1:k);
y = f(Var{:}); %no loop

カテゴリ

Help Center および File ExchangeJust for fun についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by