How can I write "n" "for loops" just by a single command?
8 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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))
0 Commenti
Risposte (1)
Walter Roberson
il 12 Dic 2013
Modificato: Walter Roberson
il 12 Dic 2013
http://www.mathworks.co.uk/matlabcentral/answers/29662-generate-points-sorted-by-distance#comment_63935 and follow the link there for the algorithm.
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
2 Commenti
Walter Roberson
il 30 Set 2020
https://www.mathworks.com/matlabcentral/answers/357969-using-recursive-function-to-calculate-all-possible-peptide-combinations#answer_282766 is probably a better link these days
Walter Roberson
il 30 Ott 2020
Even better, https://www.mathworks.com/matlabcentral/answers/623358-get-a-combination-of-unique-paths-for-given-pair-of-numbers#comment_1082638 gives code for the generalized version (entries can be different data types.)
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!