Index in position 2 exceeds array bounds. Index must not exceed 2

29 views (last 30 days)
jiamin
jiamin on 1 Apr 2022
Answered: Cris LaPierre on 1 Apr 2022
hi,everyone,
I am trying to run the code below, however I get an error in the line:"[net,YPred(:,i)] = predictAndUpdateState(net,XTest(:,i-1),'ExecutionEnvironment','cpu');"
[net,YPred] = predictAndUpdateState(net,YTrain(end));
numTimeStepsTest = numel(XTest);
for i = 2:numTimeStepsTest
[net,YPred(:,i)] = predictAndUpdateState(net,YPred(:,i-1),'ExecutionEnvironment','cpu');
end
saying that 'Index in position 2 exceeds array bounds. Index must not exceed 2'
It's a lstm net and The code in question is:
clc,clear;
%%
data1 = load("111.csv")
data = data1'
figure
plot(data,':.','Color',[0 0 180]./255,'linewidth',0.8,'Markersize',4,'MarkerFaceColor',[0 0 180]./255)
xlabel("Month")
ylabel("Cases")
title("Monthly Cases ")
numTimeStepsTrain = floor(0.9*numel(data));
dataTrain = data(1:numTimeStepsTrain+1);
dataTest = data(numTimeStepsTrain+1:end);
%%
mu = mean(dataTrain);
sig = std(dataTrain);
dataTrainStandardized = (dataTrain - mu) / sig;
%%
XTrain = dataTrainStandardized(1:end-1);
YTrain = dataTrainStandardized(2:end);
numFeatures = 1;
numResponses = 1;
numHiddenUnits = 200;
%%
layers = [ ...
sequenceInputLayer(numFeatures)
lstmLayer(numHiddenUnits)
fullyConnectedLayer(numResponses)
regressionLayer];
options = trainingOptions('adam', ...
'MaxEpochs',250, ...
'GradientThreshold',1, ...
'InitialLearnRate',0.005, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',125, ...
'LearnRateDropFactor',0.2, ...
'Verbose',0, ...
'Plots','training-progress');
%%
net = trainNetwork(XTrain,YTrain,layers,options);
%%
dataTestStandardized = (dataTest - mu) / sig;
XTest = dataTestStandardized(1:end-1);
%%
net = predictAndUpdateState(net,XTrain);
%%
[net,YPred] = predictAndUpdateState(net,YTrain(end));
numTimeStepsTest = numel(XTest);
for i = 2:numTimeStepsTest
[net,YPred(:,i)] = predictAndUpdateState(net,YPred(:,i-1),'ExecutionEnvironment','cpu');
end
%%
YPred = sig*YPred + mu;
%
YTest = dataTest(2:end);
RMSE = sqrt(mean((YPred-YTest).^2));
MAPE = mean((YPred-YTest)./YTest);
disp(["RMSE1 ",RMSE ])
disp(["MAPE1 ", MAPE])
figure
plot(dataTrain(1:end-1),'-.','Color',[0 0 255]./255,'linewidth',0.8,'Markersize',4,'MarkerFaceColor',[0 0 255]./255)
hold on
idx = numTimeStepsTrain:(numTimeStepsTrain+numTimeStepsTest+10);
plot(idx,[data(numTimeStepsTrain) YPred],'-.','Color',[255 0 0]./255,'linewidth',0.8,'Markersize',4,'MarkerFaceColor',[255 0 0]./255)
hold off
xlabel("Month")
ylabel("Cases")
title("Forecast")
legend(["Observed" "Forecast"])
%%
figure
subplot(2,1,1)
plot(YTest,'b:o','Color',[0 0 255]./255,'linewidth',0.8,'Markersize',4,'MarkerFaceColor',[0 0 255]./255)
hold on
plot(YPred,'k-s','Color',[0 0 0]./255,'linewidth',0.8,'Markersize',5,'MarkerFaceColor',[0 0 0]./255)
hold off
legend(["Observed" "Forecast"])
ylabel("Cases")
title("Forecast")
subplot(2,1,2)
stem(YPred - YTest,'filled','Color',[180 60 0]./255,'linewidth',1,'Markersize',6,'MarkerFaceColor',[180 60 0]./255)
xlabel("Month")
ylabel("Error")
title("RMSE = " + RMSE)
what should i do,
thanks a lot

Answers (1)

Cris LaPierre
Cris LaPierre on 1 Apr 2022
The error is likely from this indexing operation: XTest(:,i-1)
My guess is that XTest only has 2 columns, but your indexing in the for loop means it is trying to access data in columns that do not exist. Check the size of your variables. Perhaps you intend to index rows instead?
% Example
A = 1:2
A = 1×2
1 2
% Works
A(1,2)
ans = 2
% your error: There is no column 3 in A
A(1,3)
Index in position 2 exceeds array bounds. Index must not exceed 2.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!