필터 지우기
필터 지우기

Error: Unable to perform assignment because the left and right sides have a different number of elements.

조회 수: 1 (최근 30일)
Hello all,
i wanted to create a loop, but I always get the following error: Unable to perform assignment because the left and right sides have a different number of elements.
Here is my code:
%% Define all variables:
alpha=0.33;
beta=0.96;
n= 5; %gridsize of kgrid
kgrid= [0 0.1 0.2 0.3 0.4] ; % Choose a grid of possibloe values of the capital stock.
%To do to find the steady stae of k and choose a grid with k_steadystate in
%between. You might want to use the linspace command. Try different grid
%sizes n.
%% Make an initial guess of the value function
v0= [1 1 1 1 1]; % What is the size of v0 ?
%% Define matrix U
for j=1:n
for i=1:n
c= kgrid(i)^(alpha)-kgrid(j);%insert budget constraint
if c>0
u(i,j)= log(kgrid(i)^(alpha)-kgrid(j)) ;% for each point in the matrix use a different combination of k and k'
else
u(i,j)=-inf; %make sure that only c>0 is chosen.
end
end
end %what is happening in the loops? What happens if consumption is negative?
%% Now iterate one step to obtain v1
v1=max(u+(beta).*v0'.*ones(n));
%Insert the right characters instead of ?try to understand all digits. Check: Is v1 of the same dimension as v0?
%what we have to do step-by-step:
v2=max(u+(beta).*v1'.*ones(n));
epsilon=max(v1-v2);
v3=max(u+(beta).*v2'.*ones(n));
epsilon2=max(v3-v2);
v4=max(u+(beta).*v3'.*ones(n));
epsilon3=max(v4-v3);
v5=max(u+(beta).*v4'.*ones(n));
epsilon4=max(v5-v4);
v6=max(u+(beta).*v5'.*ones(n));
epsilon5=max(v6-v5);
%% Repeat until convergence
%set initial value
V=v1
for iter=1:10 %insert the number of iterations until convergence you may try different numbers.
%calculate next step
V(iter+1)=max(u+(beta).*V(iter)'.*ones(n))
end
I know what to do manually (see "%what we have to do step-by-step:" in the code), but somehow I don't manage to create the loop.
Maybe someone know where I did a mistake.
Thanks in advance!

채택된 답변

madhan ravi
madhan ravi 2018년 11월 18일
편집: madhan ravi 2018년 11월 18일
The error was because V keeps increasing linearly by 5 so cell is needed to contain the values properly (since they have the capaciy to expand) as below:
%% Repeat until convergence
%set initial value
V={v1}
for iter=1:10-1 %insert the number of iterations until convergence you may try different numbers.
%calculate next step
V{iter+1}=max(u+(beta).*V{iter}'.*ones(n)) ;
end
V = [V{:}]

추가 답변 (1개)

shariq khan
shariq khan 2018년 11월 18일
편집: shariq khan 2018년 11월 18일
Hey!...pay attention to multiplying mattrix dimension - a[mxn] * b[nxr] = c[mxr] - so you cant multiple v0'(5x1) with ones(5)[5x5] matrix because 1 is not equal to 5
check the dimension multiplication at every level - this is what I encountered in whole program
I didnt solve after c because this is same problem in complete program - as what I perceive
check the attachment
try solving again

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by