How to use for loop and get the result for each index varian?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello, please give me a hint or clue to solve my problem.
Look at picture number 1: I am trying to do 'for' as long as string 'VarianModel' length. Ignore my API syntax and focus to the code which i red-lined. so there will be 4 models that are going to be run. There are :
Model E, Model F, Model G, Model H.
Each model gives a result named 'Optimize value' like as shown in picture number 2. So it will be :
Optimize value (Model E) = 537.5205
Optimize value (Model F) = 561.0191
Optimize value (Model G) = 571.0191
Optimize value (Model H) = 587.5205
But when after running, it only gives the last model's result not as a cell index aray.
PICTURE #1
PICTURE #2
0 Kommentare
Akzeptierte Antwort
Voss
am 4 Mär. 2024
"after running, it only gives the last model's result"
Of course, because Optimizevalue is overwritten on each loop iteration. If you want to store one value of Optimizevalue for each loop iteration, you'll need to use indexing.
For example, if each Optimizevalue is a scalar number, you can use a 1x4 numeric vector to store them:
Nmodels = numel(VarianModel);
Optimizevalue = zeros(1,Nmodels);
for i = 1:Nmodels
% ...
% ... calculations
% ...
Optimizevalue(i) = % whatever
end
Or, another example, if each Optimizevalue is an array of potentially different size, then you can store them in a 1x4 cell array:
Nmodels = numel(VarianModel);
Optimizevalue = cell(1,Nmodels);
for i = 1:Nmodels
% ...
% ... calculations
% ...
Optimizevalue{i} = % whatever
end
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!