Info
This question is closed. Reopen it to edit or answer.
index exceeds matrix dimension
3 views (last 30 days)
Show older comments
I am coding a problem in which the answer is a matrix and values in the matrix are filled in via loops from the last row and column backwards to the first row and column. I am getting the error "index exceeds matrix dimensions" in line 48 and I don't know why. The exact line is: VNEXT(d+1) = V(SNEXT((d+1)+1,t+1));
The entire code is:
function [ V, A ] = inventory( N ) %UNTITLED3 Summary of this function goes here % Detailed explanation goes here
% initialize PARAMETERS
FIXCOST = 1; MARGCOST = 3; HCOST = 1; PRICE = 8; MAX = 3; NSTATES = MAX - 1;
STATES = (0:MAX);
DEMAND = (0:2); PROB = [0.3; 0.3; 0.4];
V = ones(NSTATES, N) * -Inf; A = zeros(NSTATES, N);
% set terminal conditions V(:, N) = 0; A(:, N) = 1;
REV = zeros(3,1); SNEXT = zeros(3,1); VNEXT = zeros(3,1);
% Backward Induction for t = N-1:-1:1 for s = 1:MAX for a = 0:MAX - s
if (a == 0)
ordercost = 0;
else
ordercost = FIXCOST + MARGCOST * a;
end
u = s + a;
holdcost = HCOST * u;
for d = 0:2
REV(d+1) = PRICE * min(u,d);
SNEXT(d+1) = max(u-d,0);
VNEXT(d+1) = V(SNEXT((d+1)+1,t+1));
end
ER = sum(REV .* PROB);
EVNEXT = sum(VNEXT .* PROB);
TOTV = ER + EVNEXT - ordercost - holdcost;
if (V(s+1,t) < TOTV)
V(s+1,t) = TOTV;
A(s+1,t) = a;
end
end
end
end
For the line in question I need to index the next row/column to use it in the calculation for the previous row/column value. So d+1 and t+1 must be used.
1 Comment
Rik
on 19 Oct 2017
Use breakpoints and go through your code line by line. Apparently you are trying to access a value that is outside of you matrix. You did (try to?) account for it already (e.g. in for t = N-1:-1:1)
Also, please format your question correctly by selecting your code and then pressing the {}Code button.
This question is closed.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!