Store results from for loop
Afficher commentaires plus anciens
How can I store the results from every iteration in a for loop?
Réponses (2)
Azzi Abdelmalek
le 5 Août 2014
0 votes
14 commentaires
civs
le 6 Août 2014
Hikaru
le 6 Août 2014
It is. The articles listed in the link are all the basic concepts you'll need to do this.
Plus, we can't really help you specifically without more specifics from you.
civs
le 6 Août 2014
Michael Haderlein
le 6 Août 2014
Modifié(e) : Michael Haderlein
le 6 Août 2014
Whatever you do in the loop will be overwritten in your last line. What are you going to to in this last line?
Hikaru
le 6 Août 2014
Like Michael said, the line
wmin_var=cell(2740,5);
at the end will overwrite whatever you have in the loop.
If mu_p is constant, you should put it before the for loop. Code will still work, just a good programming practice to put it outside.
I can't really help much without knowing the dimension of your other arrays, but this is an example of storing data from every iteration.
wmin_var=cell(2740,5);
a = size(wmin_var);
count = 0; % you don't really need this, it's here only for an example
for i=1:a(1)
for j=1:a(2)
wmin_var{i,j} = count + j; % whatever your formula is
% to calculate wmin_var
count = count + 1;
end
end
civs
le 6 Août 2014
Michael Haderlein
le 6 Août 2014
So, when I scroll through your code, some questions arise:
- In the equation, you use the Prices(i-1000:i-1). As you let i run from 1001 to length(Prices), I wonder if that's really what you want. It makes sense, if length(Prices)>1001. But then, you use only the Prices(1:length(Prices)-1000).
- Also, you write port_xx_yy_zz(i), thus, you will have 1000 zeros and only write numbers in the 1001 and later indices. Also looks as if that's not what you want.
- Third, Prices is a matrix (at least the line Prices=data(:,[2,3,4,5,6]); looks this way). When you do your calculations in the loop, you rearrange it as a vector. To see what I mean, please run "a=magic(3), a(1:5)". I guess you want it to be a vector, but most likely a vector of another kind.
- When you want to store a line vector in a matrix, you write port_xx_yy_zz(i,:)=...
Hope that these points will help you debugging your program.
Best regards,
Michael
civs
le 6 Août 2014
Michael Haderlein
le 6 Août 2014
Now we can only guess if there's further help required and if so, what it should be. My guess is that you get the error message
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
Then please see the last point of my previous comment. If not, please provide further details.
civs
le 6 Août 2014
civs
le 6 Août 2014
Michael Haderlein
le 6 Août 2014
The very unclear variable is wmin_var (and the similar ones). As you use it, it must be of size (5x1000) (you transpose it, so it shouldn't be a scalar and you use element-wise multiplication, so its transpose must be equal sized to Prices(i-1000:i-1,:)) Also, the result of this multiplication will be a matrix (1000x5). Is that what you want? I guess you want to multiply something else, but I don't know what.
civs
le 7 Août 2014
civs
le 7 Août 2014
Iain
le 7 Août 2014
what you want to do is to store your output like this:
for i = 1:X
...
result_V_min_var(i) = V_min_var;
V_eff_var_list(i) = V_eff_var_list;
... etc
end
Whatever you use as the loop number, needs to NOT change within the loop.
4 commentaires
civs
le 7 Août 2014
Iain
le 7 Août 2014
previous_value = 1000;
list_of_values(1000) = previous_value ;
for i = 1001:n
....
new_value = previous_value * (PR_min_var(i+1)+1); % At the end of the loop, "new_value" is the final value.
list_of_values(i) = new_value; % this is a list of ALL the values.
previous_value = new_value; % - so new value can change and we keep the last answer available
end
civs
le 7 Août 2014
civs
le 7 Août 2014
Catégories
En savoir plus sur Portfolio Optimization and Asset Allocation dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!