Support Vector Machines, usually called SVM, are among the most useful supervised machine learning methods for classification and regression. In MATLAB, SVM is mainly available through the Statistics and Machine Learning Toolbox, with core functions such as fitcsvm for binary classification, fitcecoc for multiclass classification, and fitrsvm for regression. MATLAB also provides app-based workflows through Classification Learner and Regression Learner.
This tutorial explains what SVM is, when to use it, how it works conceptually, and how to implement it step by step in MATLAB with practical examples.
SVM is a supervised learning algorithm that tries to find a decision boundary that separates classes as well as possible. For classification, the idea is to build a hyperplane that maximizes the margin between classes. For nonlinear problems, SVM uses a kernel function to transform the data into a space where separation becomes easier. MATLAB supports linear and kernel-based SVM workflows in its SVM training functions.
In simple terms:
SVM is often a strong choice because it:
However, SVM is not always the best option. It can become slow on very large datasets, can require parameter tuning, and is often less interpretable than a simple linear model or decision tree.
A hyperplane is the boundary used to separate classes. In 2D it is a line, in 3D it is a plane, and in higher dimensions it is called a hyperplane.
The margin is the distance between the separating boundary and the nearest training samples from each class. SVM tries to maximize this margin because a larger margin usually improves generalization.
The most important data points in SVM are the ones closest to the boundary. These are called support vectors. They define the position of the separating hyperplane.
If the data is not linearly separable, SVM can use a kernel such as:
In MATLAB, kernel-based classification is handled through fitcsvm, and multiclass SVM commonly uses fitcecoc with an SVM template.
This controls the tradeoff between having a large margin and penalizing classification errors.
SVM is sensitive to feature scale. In MATLAB, you will often use 'Standardize', true during training. MathWorks examples and options for SVM training commonly show standardization as part of the workflow.
For SVM in MATLAB, the most important functions are:
fitcsvm → binary classification SVMpredict → prediction for new observationscrossval → cross-validationfitcecoc → multiclass classification using binary learners such as SVMtemplateSVM → define SVM options to reuse inside multiclass modelsfitrsvm → SVM regressionLet us start with a very small binary classification example.
clc;
clear;
close all;
% Example data
X = [1 2;
2 3;
2 1;
3 2;
6 7;
7 8;
8 7;
7 6];
Y = [1; 1; 1; 1; 2; 2; 2; 2];
% Train linear SVM
Mdl = fitcsvm(X, Y, ...
'KernelFunction', 'linear', ...
'Standardize', true);
% Predict labels on training data
[label, score] = predict(Mdl, X);
disp('Predicted labels:');
disp(label);
disp('Scores:');
disp(score);Here:
X contains the input features,Y contains the class labels,fitcsvm trains the SVM classifier,'KernelFunction','linear' chooses a linear SVM,'Standardize',true scales the predictors before training,predict gives the predicted class labels.predict returns class labels for classification SVM models in MATLAB.
gscatter(X(:,1), X(:,2), Y, 'rb', 'ox');
xlabel('Feature 1');
ylabel('Feature 2');
title('Training Data');
grid on;This allows you to see whether the two classes look linearly separable.
In practice, we do not evaluate the model only on training data.
clc;
clear;
close all;
% Data
X = [1 2;
2 3;
2 1;
3 2;
6 7;
7 8;
8 7;
7 6;
1.5 2.5;
6.5 7.5];
Y = [1;1;1;1;2;2;2;2;1;2];
% Random split
rng(1);
cv = cvpartition(Y, 'HoldOut', 0.3);
XTrain = X(training(cv), :);
YTrain = Y(training(cv), :);
XTest = X(test(cv), :);
YTest = Y(test(cv), :);
% Train model
Mdl = fitcsvm(XTrain, YTrain, ...
'KernelFunction', 'linear', ...
'Standardize', true);
% Test prediction
YPred = predict(Mdl, XTest);
% Accuracy
accuracy = mean(YPred == YTest) * 100;
fprintf('Test Accuracy = %.2f%%\n', accuracy);This introduces a basic validation strategy. In real projects, this is much better than checking only the training set.
A confusion matrix shows how many examples were correctly or incorrectly classified.
cm = confusionmat(YTest, YPred);
disp('Confusion Matrix:');
disp(cm);
confusionchart(YTest, YPred);
title('Confusion Matrix');Some datasets are not linearly separable. In such cases, an RBF kernel is often a good starting point.
clc;
clear;
close all;
% XOR-like dataset
X = [0 0;
0 1;
1 0;
1 1;
0.1 0.2;
0.2 0.9;
0.9 0.1;
0.8 0.8];
Y = [1; 2; 2; 1; 1; 2; 2; 1];
% Train nonlinear SVM
Mdl = fitcsvm(X, Y, ...
'KernelFunction', 'rbf', ...
'KernelScale', 'auto', ...
'Standardize', true);
YPred = predict(Mdl, X);
accuracy = mean(YPred == Y) * 100;
fprintf('Accuracy = %.2f%%\n', accuracy);The RBF kernel is useful when the boundary between classes is curved or complex. MATLAB supports kernel settings such as kernel function and kernel scale in SVM training. Hyperparameter optimization can also tune choices such as KernelFunction, KernelScale, BoxConstraint, and related options.
Cross-validation gives a more reliable estimate of model performance than a single train/test split.
clc;
clear;
close all;
load ionosphere
% Train binary SVM
SVMModel = fitcsvm(X, Y, ...
'Standardize', true, ...
'KernelFunction', 'rbf', ...
'KernelScale', 'auto');
% 5-fold cross-validation
CVSVMModel = crossval(SVMModel, 'KFold', 5);
% Classification loss
classLoss = kfoldLoss(CVSVMModel);
fprintf('5-Fold Cross-Validation Loss = %.4f\n', classLoss);
fprintf('5-Fold Cross-Validation Accuracy = %.2f%%\n', (1-classLoss)*100);MathWorks documents the use of crossval with SVM models, including examples with the ionosphere dataset and standardized predictors.
SVM performance depends strongly on its parameters. Important ones include:
MATLAB supports hyperparameter optimization directly in fitcsvm.
clc;
clear;
close all;
load ionosphere
Mdl = fitcsvm(X, Y, ...
'Standardize', true, ...
'OptimizeHyperparameters', {'BoxConstraint','KernelScale','KernelFunction'}, ...
'HyperparameterOptimizationOptions', struct( ...
'AcquisitionFunctionName', 'expected-improvement-plus', ...
'ShowPlots', false));
YPred = predict(Mdl, X);
acc = mean(YPred == Y) * 100;
fprintf('Training Accuracy = %.2f%%\n', acc);MathWorks explicitly states that fitcsvm can optimize hyperparameters such as BoxConstraint, KernelFunction, KernelScale, PolynomialOrder, and Standardize using optimization workflows.
The ionosphere dataset is a classic binary classification dataset often used in MATLAB examples.
clc;
clear;
close all;
load ionosphere
% Split data
rng(10);
cv = cvpartition(Y, 'HoldOut', 0.25);
XTrain = X(training(cv), :);
YTrain = Y(training(cv), :);
XTest = X(test(cv), :);
YTest = Y(test(cv), :);
% Train SVM
Mdl = fitcsvm(XTrain, YTrain, ...
'KernelFunction', 'rbf', ...
'KernelScale', 'auto', ...
'Standardize', true);
% Predict on test set
YPred = predict(Mdl, XTest);
% Accuracy
accuracy = mean(YPred == YTest) * 100;
fprintf('Test Accuracy = %.2f%%\n', accuracy);
% Confusion matrix
confusionchart(YTest, YPred);
title('Ionosphere SVM Classification');A standard SVM is naturally designed for binary classification. For multiclass problems, MATLAB uses Error-Correcting Output Codes (ECOC) through fitcecoc. By default, for K classes, one-versus-one coding uses K(K−1)/2 binary learners.
clc;
clear;
close all;
load fisheriris
X = meas;
Y = species;
% Define SVM learner template
t = templateSVM('KernelFunction', 'rbf', ...
'KernelScale', 'auto', ...
'Standardize', true);
% Train multiclass ECOC model
Mdl = fitcecoc(X, Y, 'Learners', t);
% Predict
YPred = predict(Mdl, X);
% Accuracy
accuracy = mean(strcmp(YPred, Y)) * 100;
fprintf('Training Accuracy = %.2f%%\n', accuracy);
% Confusion chart
confusionchart(Y, YPred);
title('Iris Multiclass SVM');
Here:
templateSVM defines the SVM settings,fitcecoc creates a multiclass model,MathWorks documents templateSVM for creating reusable SVM learner templates and fitcecoc for multiclass classification using SVM learners.
clc;
clear;
close all;
load fisheriris
X = meas;
Y = species;
rng(2);
cv = cvpartition(Y, 'HoldOut', 0.3);
XTrain = X(training(cv), :);
YTrain = Y(training(cv), :);
XTest = X(test(cv), :);
YTest = Y(test(cv), :);
t = templateSVM('KernelFunction', 'rbf', ...
'KernelScale', 'auto', ...
'Standardize', true);
Mdl = fitcecoc(XTrain, YTrain, 'Learners', t);
YPred = predict(Mdl, XTest);
accuracy = mean(strcmp(YPred, YTest)) * 100;
fprintf('Test Accuracy = %.2f%%\n', accuracy);
confusionchart(YTest, YPred);
title('Multiclass SVM Test Results');
SVM is not only for classification. It can also be used for regression, where the goal is to predict a numeric value. MATLAB uses fitrsvm for this. For low- to moderate-dimensional data, fitrsvm is the standard SVM regression function; for high-dimensional linear regression cases, MathWorks points users toward fitrlinear.
clc;
clear;
close all;
% Create sample data
X = (1:10)';
Y = [1.2; 1.9; 3.1; 4.2; 5.0; 5.8; 7.1; 8.2; 8.9; 10.1];
% Train SVM regression model
Mdl = fitrsvm(X, Y, ...
'KernelFunction', 'gaussian', ...
'Standardize', true);
% Prediction
XTest = (1:0.1:10)';
YPred = predict(Mdl, XTest);
% Plot
plot(X, Y, 'o');
hold on;
plot(XTest, YPred, 'LineWidth', 2);
xlabel('X');
ylabel('Y');
title('SVM Regression');
legend('Training Data', 'SVR Prediction');
grid on;
For regression, accuracy is not used. Instead, we often compute:
Example:
clc;
clear;
close all;
X = (1:10)';
Y = [1.2; 1.9; 3.1; 4.2; 5.0; 5.8; 7.1; 8.2; 8.9; 10.1];
Mdl = fitrsvm(X, Y, ...
'KernelFunction', 'gaussian', ...
'Standardize', true);
YPred = predict(Mdl, X);
MAE = mean(abs(Y - YPred));
MSE = mean((Y - YPred).^2);
RMSE = sqrt(MSE);
R2 = 1 - sum((Y - YPred).^2) / sum((Y - mean(Y)).^2);
fprintf('MAE = %.4f\n', MAE);
fprintf('MSE = %.4f\n', MSE);
fprintf('RMSE = %.4f\n', RMSE);
fprintf('R^2 = %.4f\n', R2);clc;
clear;
close all;
% Synthetic nonlinear regression data
x = linspace(-3, 3, 100)';
y = sin(x) + 0.2*randn(size(x));
% Train/test split
rng(3);
cv = cvpartition(length(x), 'HoldOut', 0.2);
XTrain = x(training(cv), :);
YTrain = y(training(cv), :);
XTest = x(test(cv), :);
YTest = y(test(cv), :);
% Train SVR
Mdl = fitrsvm(XTrain, YTrain, ...
'KernelFunction', 'gaussian', ...
'KernelScale', 'auto', ...
'Standardize', true);
% Predict
YPred = predict(Mdl, XTest);
% Error metrics
MSE = mean((YTest - YPred).^2);
RMSE = sqrt(MSE);
fprintf('Test RMSE = %.4f\n', RMSE);
% Plot
plot(XTrain, YTrain, 'o');
hold on;
plot(XTest, YPred, '.', 'MarkerSize', 15);
xlabel('x');
ylabel('y');
title('Nonlinear SVM Regression');
legend('Training Data', 'Predictions');
grid on;If you prefer a graphical interface, MATLAB provides Classification Learner. It lets you import data, choose classifiers, validate models, optimize hyperparameters, and inspect results. For two-class SVM it uses fitcsvm; for multiclass classification it uses fitcecoc. It can also generate MATLAB code from the trained workflow.
Typical steps:
Type:
classificationLearnerThis is very useful for beginners because it reduces coding effort and helps compare models quickly.
For regression problems, MATLAB provides Regression Learner. It uses fitrsvm for SVM regression models and lets you configure kernel and box-constraint-related options from the app interface.
Typical use:
regressionLearnerThen:
SVM usually performs much better when predictors are on similar scales. That is why using:
'Standardize', trueis strongly recommended in many SVM workflows. MathWorks examples and templates repeatedly include standardized predictors in SVM training.
A simple rule:
A model that performs perfectly on training data may still fail on new unseen data. Use:
MATLAB provides built-in cross-validation support for SVM models.
For good SVM performance, tune:
BoxConstraintKernelScaleKernelFunctionPolynomialOrderMathWorks documents these as eligible hyperparameters for optimization in fitcsvm, and SVM-related options are also exposed through the learner apps.
Remember:
fitcsvm is mainly for binary classification and also supports one-class workflows,fitcecoc is used for multiclass classification with SVM learners. This is one of the most common beginner mistakes in MATLAB SVM work.
Here is a more complete project-style workflow.
clc;
clear;
close all;
%% Load data
load fisheriris
X = meas;
Y = species;
%% Explore data
disp('First 5 observations:');
disp(X(1:5,:));
disp('First 5 labels:');
disp(Y(1:5));
%% Train/test split
rng(42);
cv = cvpartition(Y, 'HoldOut', 0.2);
XTrain = X(training(cv), :);
YTrain = Y(training(cv), :);
XTest = X(test(cv), :);
YTest = Y(test(cv), :);
%% Define learner
t = templateSVM( ...
'KernelFunction', 'rbf', ...
'KernelScale', 'auto', ...
'Standardize', true);
%% Train model
Mdl = fitcecoc(XTrain, YTrain, 'Learners', t);
%% Predict
YPred = predict(Mdl, XTest);
%% Evaluate
accuracy = mean(strcmp(YPred, YTest)) * 100;
fprintf('Test Accuracy = %.2f%%\n', accuracy);
%% Confusion chart
figure;
confusionchart(YTest, YPred);
title('Iris Multiclass SVM');
%% Predict a new flower
newSample = [5.1 3.5 1.4 0.2];
predictedClass = predict(Mdl, newSample);
disp('Predicted class for new sample:');
disp(predictedClass);This project includes:
That is very close to what you would do in a real beginner project.
fitcsvm for three or more classesFor multiclass classification, use fitcecoc with templateSVM, not only fitcsvm.
If one feature ranges from 0 to 1 and another from 0 to 10000, SVM can behave poorly.
Always validate on unseen data.
Start simple, then move to nonlinear kernels if needed.
Default settings are not always optimal.
SVM is a good choice when:
You might avoid SVM when:
SVM is one of the most important supervised learning methods and MATLAB makes it relatively easy to use. For binary classification, fitcsvm is the main function. For multiclass classification, MATLAB uses fitcecoc with SVM learners. For regression, fitrsvm is the standard approach. MATLAB also provides learner apps that help train, compare, validate, and export SVM models visually.
A strong practical workflow is:
Mdl = fitcsvm(X, Y, 'KernelFunction', 'rbf', 'Standardize', true);
YPred = predict(Mdl, XTest);
t = templateSVM('KernelFunction', 'rbf', 'Standardize', true);
Mdl = fitcecoc(X, Y, 'Learners', t);
YPred = predict(Mdl, XTest);Mdl = fitrsvm(X, Y, 'KernelFunction', 'gaussian', 'Standardize', true);
YPred = predict(Mdl, XTest);CVSVMModel = crossval(Mdl, 'KFold', 5);
loss = kfoldLoss(CVSVMModel);
fisheriris dataset and build a multiclass SVM model
crossval to estimate model performance
fitrsvm
Linear SVM for binary classification.
Comparison between linear SVM and RBF SVM.
Multiclass SVM with fitcecoc on iris data.
Performance estimation with cross-validation.
SVM regression with fitrsvm.