Hi,
I am trying to do a sort of brute-force optimisation method on my script. I have 3 parameters, and I want to use 3 values for each of those 3 parameters.
I want to script to run with all possible values, giving a total of 27 results.
What is the best way of doing this? Should I just use 3 nested "for" loops?
Thanks

 Respuesta aceptada

Matt J
Matt J el 30 de Ag. de 2021
Editada: Matt J el 30 de Ag. de 2021
You can generate a matrix with all combinations using ndgrid, e.g.,
[p1,p2,p3]=ndgrid(1:3,10:10:30,100:100:300 );
Combinations = [p1(:),p2(:),p3(:)]
Combinations = 27×3
1 10 100 2 10 100 3 10 100 1 20 100 2 20 100 3 20 100 1 30 100 2 30 100 3 30 100 1 10 200
Should I just use 3 nested "for" loops?
You could, or you could use a single loop over the rows of Combinations. Ideally, though, you would look for some vectorized way to process the entire Combinations matrix.

3 comentarios

TOK
TOK el 30 de Ag. de 2021
Thank you, this is something I wasn't aware of and will definitely be using!
I suppose I then have a single "for" loop for my whole script which then accesses the data using the indexing of the matrix?
Matt J
Matt J el 30 de Ag. de 2021
Yes, you could do that.
TOK
TOK el 3 de Sept. de 2021
Thanks for your help with this Matt! In the end I just used nested for loops in my case, as it gave me the flexibility to specify a different number of values for each of my 3 parameters.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Versión

R2020a

Etiquetas

Preguntada:

TOK
el 30 de Ag. de 2021

Comentada:

TOK
el 3 de Sept. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by