Neural Network Toolbox Example Dataset Error - Index Exceeds Matrix Dimensions

G
Gretchen · Jun 18, 2021 · 2K views
Question
I am trying to learn how to use the neural network toolbox and thought that the best way to do that would be to work through the examples in the first section of the toolbox documentation (i.e. "Getting Started"). However, with the exception of the simplefit dataset, every time I try to use one of the given data sets, Matlab returns "Index Exceeds Matrix Dimensions." For example, if done in the command-line, this is what it looks like: >> load house_dataset >> net=newfit(houseInputs,houseTargets,20); ??? Index exceeds matrix dimensions. Error in ==> initnw at 113 range(inputStart(j):inputStop(j),:) = temp((inputStart(j):inputStop(j))-inputStart(j)+1,:); Error in ==> initlay at 73 net = feval(initFcn,net,i); Error in ==> network.init at 68 net = feval(initFcn,net); Error in ==> newff>new_5p1 at 188 net = init(net); Error in ==> newff at 89 net = new_5p1(varargin{:}); Error in ==> newfit at 67 net = newff(varargin{:}); These are exactly the same commands that are given in the documentation (under "Fitting a Function" and then "Using Command-Line Functions").   If I try to access the data using the neural networking GUI, the error message comes up in between the Network Size and Train Network steps. Everything I've done has been exactly how the manual describes; these examples are basic enough that there doesn't seem to be much I could have done to mess it up. I've tried the function fitting, pattern recognition, and clustering examples and they've all failed in the same way.   Could someone who understands the programming of the toolbox explain why this error is coming up here and what I can try to fix it?
Expert Answer
Profile picture of Kshitij Singh
Kshitij Singh PhD Expert
Answered Sep 9, 2026






Fixing "Index Exceeds Matrix Dimensions" in MATLAB Neural Networks


This error occurs in MATLAB Neural Network Toolbox (Deep Learning Toolbox) when data orientations, sample counts, or target formats do not match the dimensions expected by the network.

1. Cause #1: Samples vs. Features Orientation (Most Common)


MATLAB standard neural networks (fitnet, patternnet, feedforwardnet) require features in rows and samples in columns.


Wrong: Passing X as [N_samples x N_features] and Y as [N_samples x N_outputs].


Fix: Transpose both inputs and targets before training.

% Suppose X has 500 samples and 4 features (500x4)
% Suppose Y has 500 target values (500x1)

% Transpose to [Features x Samples]
X_train = X'; % Now 4 x 500
Y_train = Y'; % Now 1 x 500

net = fitnet(10);
net = train(net, X_train, Y_train);

2. Cause #2: Incorrect Indexing of Training Record (tr)


When you extract partition indices (tr.trainInd, tr.valInd, tr.testInd), remember that samples are stored in columns.

% Correct: Index columns
train_X = X_train(:, tr.trainInd);
train_Y = Y_train(:, tr.trainInd);

% Wrong: Indexing rows causes "Index exceeds matrix dimensions"
% train_X = X_train(tr.trainInd, :);

3. Cause #3: Target Label Format in Classification (patternnet)


For multi-class classification with \(C\) classes, patternnet requires a one-hot encoded matrix of size [C x N_samples], not a single vector of integer class labels (1, 2, 3).

% If your labels are class indices: labels = [1; 2; 3; 1; 2; ...]; (1 x N)
% Convert to one-hot binary vectors using ind2vec:
Y_onehot = full(ind2vec(labels)); % Produces [NumClasses x NumSamples]

net = patternnet(10);
net = train(net, X_train, Y_onehot);

4. Cause #4: Sample Count Mismatch Between X and Y


The number of columns in X and Y must be identical.

% Check dimensions before training
fprintf('X size: %d features x %d samples\n', size(X_train, 1), size(X_train, 2));
fprintf('Y size: %d outputs  x %d samples\n', size(Y_train, 1), size(Y_train, 2));

if size(X_train, 2) ~= size(Y_train, 2)
    error('Number of samples in X and Y must match.');
end

Quick Dimension Reference Table












ObjectExpected ShapeDescription
Input Matrix X[NumFeatures x NumSamples]Each column is one data point.
Regression Target Y[NumOutputs x NumSamples]Each column is the target output.
Classification Target Y[NumClasses x NumSamples]One-hot binary encoding (0s and 1s).
Output Predictions Y_hat[NumOutputs x NumSamples]Network evaluation output.



100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!