Question
Hello I am trying to use neural network to make some prediction based on my input and target data. I have read all related tutorial in Matlab and also looked at the matlab examples. I kinda learned how to develop a network but I dont know how to use this train network to make some prediction ? is there any code that im missing ? does anyone have a sample script that can be shared here? that's what I have, for example : x=[1 2 3;4 5 3] t=[0.5 0.6 0.7] , net=feedforwardnet(10) , net=train(net,x,t) , perf=perform(net,y,t) how can I predict the output for a new set of x (xprime=[4 2 3;4 7 8]) based on this trained network? thanks
Expert Answer
John Williams
PhD Expert
Answered Sep 17, 2026
To predict from a trained neural network in MATLAB, use classify(net, new_data) for classification tasks to get class labels, or predict(net, new_data) for regression tasks and raw probability scores. If using a shallow neural network (created with fitnet or patternnet), evaluate inputs directly by calling the network object like a function: output = net(input_matrix).
Core Methods Based on Network Type
- Deep Classification Networks (DAGNetwork / SeriesNetwork): Use
classify()to return categorical class predictions directly, or[labels, scores] = classify(net, X)to inspect the softmax probability distribution across classes. - Deep Regression Networks: Use
YPred = predict(net, X)to generate continuous numerical estimates. - Shallow Neural Networks: Use
Y = net(X)orY = sim(net, X), whereXhas features along rows and observations along columns.
Executable MATLAB Code: End-to-End Prediction Pipeline
% =========================================================================
% METHOD 1: Predicting with Modern Deep Learning Networks (Images or Tables)
% =========================================================================
% 1. Load a pre-trained network from disk or workspace
loadedModel = load('trainedModel.mat');
net = loadedModel.net; % Contains trained DAGNetwork or SeriesNetwork
% 2. Read and prepare a new test sample
testImage = imread('new_sample.jpg');
% Match network input dimensions (e.g., 224x224 RGB)
inputSize = net.Layers(1).InputSize(1:2);
preprocessedImage = imresize(testImage, inputSize);
% 3. Run inference
% Option A: For categorical class prediction
[predictedLabel, classProbabilities] = classify(net, preprocessedImage);
% Option B: For continuous numerical regression
% predictedValue = predict(net, preprocessedImage);
fprintf('Predicted Class: %s (Confidence: %.2f%%)\n', ...
string(predictedLabel), max(classProbabilities) * 100);
% =========================================================================
% METHOD 2: Predicting with Shallow Neural Networks (feedforwardnet/patternnet)
% =========================================================================
% Suppose 'shallowNet' was trained with 5 input features
% Shape: [numFeatures x numSamples] -> Note: columns are observations
newSample = [12.4; 3.2; 0.85; 110.0; 4.5];
% If you applied normalization (e.g., mapminmax) during training, apply it here:
% newSampleNormalized = mapminmax('apply', newSample, trainingSettings);
% Run prediction
rawPrediction = net(newSample);
% For pattern recognition networks, find class with highest response
[~, classIndex] = max(rawPrediction);
fprintf('Shallow Network Predicted Class Index: %d\n', classIndex);
Crucial Engineering Checklist Before Running Predictions
- Input Dimension Alignment: Deep CNNs expect a 4D array for batches
[Height, Width, Channels, BatchSize]. When passing a single image, MATLAB allows a 3D array[Height, Width, Channels], but tabular inputs to feature networks must strictly match the layer training dimensions. - Consistent Preprocessing: If your network was trained on zero-mean normalized inputs or pixel values scaled to
[0, 1], unscaled inputs (such as rawuint8images ranging from 0 to 255) will produce completely incorrect predictions. Apply the exact same transformation function. - GPU Acceleration for Batch Inference: When evaluating thousands of test records or video frames simultaneously, pass
'ExecutionEnvironment', 'gpu'intopredict()to accelerate throughput via CUDA cores.
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
Related neural network Questions & Solutions
Browse All →
Explore similar technical troubleshooting questions and verified MATLAB solutions:
Ready-to-Run MATLAB & Simulink Projects
Browse All Projects →