Info

この質問は閉じられています。 編集または回答するには再度開いてください。

index exceeds matrix dimension

1 回表示 (過去 30 日間)
Raymond Waters
Raymond Waters 2017 年 10 月 19 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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 件のコメント
Rik
Rik 2017 年 10 月 19 日
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.

回答 (1 件)

KL
KL 2017 年 10 月 19 日
編集済み: KL 2017 年 10 月 19 日
When d = 2,
V(SNEXT((d+1)+1,t+1))
means
V(SNEXT(4,t+1)) %there's no 4th row
and I don't even have to say about 't', while SNEXT is simply a column vector so, t should never be more than 0. So there you get the error.

この質問は閉じられています。

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by