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

조회 수: 5 (최근 30일)
Gengxuan Shang
Gengxuan Shang 2018년 4월 24일
편집: Gengxuan Shang 2018년 4월 26일
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?

답변 (1개)

Ahmet Cecen
Ahmet Cecen 2018년 4월 24일
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
Gengxuan Shang
Gengxuan Shang 2018년 4월 26일
편집: Gengxuan Shang 2018년 4월 26일
@Ahmet Cecen ExpectedReturn should be a 1x1 double, not sure why but sometimes it outputs an empty vector.
Gengxuan Shang
Gengxuan Shang 2018년 4월 26일
편집: Gengxuan Shang 2018년 4월 26일
@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?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by