Effacer les filtres
Effacer les filtres

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

8 vues (au cours des 30 derniers jours)
Ehsan
Ehsan le 12 Déc 2013
Commenté : Walter Roberson le 30 Oct 2020
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))

Réponses (1)

Walter Roberson
Walter Roberson le 12 Déc 2013
Modifié(e) : Walter Roberson le 12 Déc 2013
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

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by