how to run different for loops in MATLAB
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
lorenzo vallerini
il 24 Nov 2021
Risposto: Alan Weiss
il 26 Nov 2021
PEMAX = [10 20 25 30 35 40];
CNOM = [50 75 90 100 125 150];
PBMAX = [20 30 40 50 60 70];
for k=1:length(PEMAX)
Pemax=PEMAX(k);
for z=1:length(PBMAX)
Pbmax=PBMAX(z);
for q=1:length(CNOM)
Cnom=CNOM(q);
Matlaboptimization
end
end
end
Hello everyone, I am trying to make the "Matlaboptimization" program work for the different values of Pemx, Pbmax and Cnom that I have defined. The problem I have encountered so far is that the program only works for the first 3 values of my variables, without continuing with all the other simulations (being 3 variables with 6 possible values I should have 6*6*6 simulations performed). Also, I don't know how to save every result of my simulations, I would like to save each of these separately, is that feasible? thanks for your help
0 Commenti
Risposta accettata
Alan Weiss
il 26 Nov 2021
Usually, people write functions that accept arguments and return arguments, such as
result(k,z,q) = Matlaboptimization(Pemax,Pbmax,Cnom);
You would put that line inside your nested for loops, and at the end you would have a result array holding all the results. You should also write a line just before your nested for loops allocating the result array, such as
result = zeros(length(PEMAX),length(PBMAX),length(CNOM));
Alan Weiss
MATLAB mathematical toolbox documentation
0 Commenti
Più risposte (1)
Voss
il 24 Nov 2021
I'm not sure how Matlaboptimization.m gets called at all; you should call it in your code without the .m extension, i.e.:
PEMAX = [10 20 25 30 35 40];
CNOM = [50 75 90 100 125 150];
PBMAX = [20 30 40 50 60 70];
for k=1:length(PEMAX)
Pemax=PEMAX(k);
for z=1:length(PBMAX)
Pbmax=PBMAX(z);
for q=1:length(CNOM)
Cnom=CNOM(q);
Matlaboptimization
end
end
end
I assume Matlaboptimization is a script that relies on variables called Pemax, Pbmax, and Cnom. One way to save all the results separately is to add some code immediately after the call to Matlaboptimization that does that, saving to a .mat file with a file name like "results_Pemax=10_Pbmax=20_Cnom=50.mat", for instance. Whatever name makes sense to you and will allow you to know which run it corresponds to when you have to use them later. Another way would be to have Matlaboptimization do the saving itself.
Regardless, I think you should get 6*6*6 = 216 sets of results, not 6! = 720.
Vedere anche
Categorie
Scopri di più su Parallel Computing Fundamentals 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!