In an assignment A(:) = B, the number of elements in A and B must be the same.

My problem here is that if I were to run the following code without the for loop and a fixed value for rtemp, then the variable ExpectedReturn is a 1x1 double.
ExpectedReturn = zeros(size(r));
for i = 1:size(r)
Rtemp = r(i);
b = ones(25,1).*(Rtemp*C);
b = [b;C];
[xLP,FVAL,EXITFLAG]=linprog(f,A,b,Aeq,beq,LB,UB,[],options);
ExpectedReturn(i)=-1*round(FVAL/0.01)*0.01;
end
However, if I were to run it as is, with the value of rtemp varying, I get this error:
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in PortfolioOpt (line 54)
ExpectedReturn(i)=-1*round(FVAL/0.01)*0.01;
Since I am storing a 1x1 double in ExpectedReturn(i), why does it say the dimensions don't match?

Respuestas (1)

Just convert that to:
ExpectedReturn = cell(size(r));
%...
ExpectedReturn{i}=-1*round(FVAL/0.01)*0.01;
and let it run, then you can come back and see why this is happening. I can't really suggest anything more without seeing a code I can run directly.

7 comentarios

Thanks a lot for this answer! Now it actually does something. For some reason now the resulting cell has only 1 field with a double, and for the rest there is an empty vector....would you happen to have any idea why that might be? This is the same code in context:

 R = -0.1:0.001:0.05;
N=10;
T=25;
C=1000;
f = -yBar;
A = -training;
A = [A;ones(1,10)];
Aeq = zeros(1,10);
beq = zeros(1,1);
LB = 0;
UB = 0.5*C;
options = optimset('Display','off');
b = zeros(26,1);
ExpectedReturn = cell(size(R));
 for j = 1:size(R)
    Rtemp = R(j);
    b(1:25,1) = -(Rtemp*C);
    b(26,1) = C;
    [xLP,FVAL,EXITFLAG]=linprog(f,A,b,Aeq,beq,LB,UB,[],options);
    ExpectedReturn{i}=-1*round(FVAL/0.01)*0.01;  %report to two decimal places
end 

your loop index is called j your cell index i

Oh, thanks! Sorry, didn't realise. I fixed it but the problem is still there...
What does ExpectedReturn look like?
Gengxuan Shang
Gengxuan Shang el 26 de Abr. de 2018
Editada: Gengxuan Shang el 26 de Abr. de 2018
@Ahmet Cecen ExpectedReturn should be a 1x1 double, not sure why but sometimes it outputs an empty vector.
Gengxuan Shang
Gengxuan Shang el 26 de Abr. de 2018
Editada: Gengxuan Shang el 26 de Abr. de 2018
@Dennis Thank you so much! It outputs things now. Could you explain why this might be? As far as I know, wouldn't size() normally work for a vector without the second argument?

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 24 de Abr. de 2018

Editada:

el 26 de Abr. de 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by