How to make variables in a while loop be an array
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Could anybody help me with this please? I am trying to work out a optimisation problem. I am using a while loop. I am able to obtain results in an array e.g. result(n).however, I am not able to make variable "n" as an array,hence, the value that I can access is always the last n value appeared. I would like to know how to make the variables into an array form that I can access the optimal result,aslo the corresponding variable "n" value.
3 Kommentare
Akzeptierte Antwort
Walter Roberson
am 2 Nov. 2011
idx = 0;
while ....
n = ....
result(n) = ....
idx = idx + 1; %!
all_n(idx) = n; %!
end
Then later you can (for example)
for K = 1 : idx
this_n = all_n(idx);
disp(result(this_n))
end
4 Kommentare
Walter Roberson
am 2 Nov. 2011
[maxvalue, maxidx] = max(result);
Then the index of the maximum value is maxidx
This does not require that the n be recorded.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!