Store results from for loop
이전 댓글 표시
How can I store the results from every iteration in a for loop?
답변 (2개)
Azzi Abdelmalek
2014년 8월 5일
0 개 추천
댓글 수: 14
civs
2014년 8월 6일
Hikaru
2014년 8월 6일
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
2014년 8월 6일
Michael Haderlein
2014년 8월 6일
편집: Michael Haderlein
2014년 8월 6일
Whatever you do in the loop will be overwritten in your last line. What are you going to to in this last line?
Hikaru
2014년 8월 6일
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
2014년 8월 6일
Michael Haderlein
2014년 8월 6일
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
2014년 8월 6일
Michael Haderlein
2014년 8월 6일
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
2014년 8월 6일
civs
2014년 8월 6일
Michael Haderlein
2014년 8월 6일
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
2014년 8월 7일
civs
2014년 8월 7일
Iain
2014년 8월 7일
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
civs
2014년 8월 7일
Iain
2014년 8월 7일
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
2014년 8월 7일
civs
2014년 8월 7일
카테고리
도움말 센터 및 File Exchange에서 Portfolio Optimization and Asset Allocation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!