Artificial Intelligence (AI) using MATLAB
Artificial Intelligence (AI) in MATLAB provides powerful tools and functions to build intelligent systems. MATLAB offers functionalities for machine learning, deep learning, computer vision, natural language processing, and reinforcement learning.
Getting Started with AI in MATLAB
MATLAB’s AI capabilities are available through specialized toolboxes like Deep Learning Toolbox
, Statistics and Machine Learning Toolbox
, and Reinforcement Learning Toolbox
. You can install these from MATLAB’s Add-Ons Manager.
Example 1: Linear Regression using AI
Linear regression predicts a dependent variable (y) from one or more independent variables (x). It is represented as:
y = mx + c
where m
is the slope and c
is the intercept.
% Linear Regression in MATLAB
x = [1 2 3 4 5]; % Independent variable
y = [2.2 4.1 6.0 8.1 10.1]; % Dependent variable
coeffs = polyfit(x, y, 1); % Fit linear model
y_pred = polyval(coeffs, x); % Predicted y values
disp(coeffs); % Display slope and intercept
plot(x, y, 'o', x, y_pred, '-'); % Plot data and model
Example 2: Neural Networks
Neural networks are fundamental to AI for tasks like classification, regression, and clustering. A simple feedforward neural network can be trained in MATLAB:
% Simple Neural Network
inputs = [0 1 2 3 4; 5 6 7 8 9]; % Input data
targets = [0 1 1 0 1]; % Target outputs
net = feedforwardnet(10); % Create network with 10 neurons
net = train(net, inputs, targets); % Train network
outputs = net(inputs); % Predict outputs
disp(outputs);
Types of AI Problems and MATLAB Solutions
AI problems are generally classified into supervised learning, unsupervised learning, reinforcement learning, and natural language processing. Here are some examples:
Supervised Learning
Supervised learning involves labeled data for training models. Common algorithms include Support Vector Machines (SVM), Decision Trees, and Neural Networks.
% Classification using SVM
load fisheriris; % Load dataset
svmModel = fitcsvm(meas, species); % Train SVM model
disp(svmModel);
Unsupervised Learning
Unsupervised learning identifies hidden patterns in data without labeled outputs. Clustering is a common task in this category.
% K-Means Clustering
data = rand(100, 2); % Generate random data
[idx, C] = kmeans(data, 3); % Perform clustering into 3 clusters
gscatter(data(:,1), data(:,2), idx); % Plot clusters
disp(C); % Display cluster centroids
Reinforcement Learning
Reinforcement learning trains agents to make decisions by interacting with an environment.
% Training a Reinforcement Learning Agent
env = rlPredefinedEnv("CartPole-Continuous"); % Load example environment
obsInfo = getObservationInfo(env); % Environment details
actInfo = getActionInfo(env);
disp(obsInfo);
disp(actInfo);
Example 3: Handwritten Digit Classification using SVM
Supervised learning can be used to classify handwritten digits using MATLAB’s fitcsvm
function. In this example, we use the MNIST
dataset, a popular dataset for digit recognition, and train a Support Vector Machine (SVM) classifier.
% Step 1: Load Dataset
load('digitDataset.mat'); % Load MNIST dataset
% Step 2: Prepare Data
X = images; % Features: Flattened image pixels
y = labels; % Labels: Corresponding digit class
% Step 3: Split Data into Training and Test Sets
cv = cvpartition(y, 'HoldOut', 0.3);
XTrain = X(cv.training, :); % Training features
yTrain = y(cv.training); % Training labels
XTest = X(cv.test, :); % Test features
yTest = y(cv.test); % Test labels
% Step 4: Train the SVM Classifier
svmModel = fitcsvm(XTrain, yTrain, 'KernelFunction', 'linear', 'Standardize', true);
% Step 5: Evaluate the Model
yPred = predict(svmModel, XTest); % Predict test set
accuracy = sum(yPred == yTest) / numel(yTest); % Calculate accuracy
disp(['Accuracy: ', num2str(accuracy * 100), '%']);
% Step 6: Visualize Results
figure;
confusionchart(yTest, yPred); % Plot confusion matrix
Explanation of the Code
- Dataset: The MNIST dataset consists of 28×28 grayscale images of digits (0–9).
- Training and Testing: The dataset is split into 70% for training and 30% for testing.
- Model: An SVM with a linear kernel is used for classification.
- Accuracy: The model’s performance is evaluated using the accuracy metric and a confusion matrix.
Output
The following is an example of the output for the trained model:
Accuracy: 92.5%
A confusion matrix is also displayed, showing the performance of the classifier for each digit.
Example 4: Customer Segmentation using K-Means Clustering
Unsupervised learning techniques like K-Means Clustering are widely used for tasks such as customer segmentation. In this example, we will segment customers based on their purchasing behavior using MATLAB’s kmeans
function.
% Step 1: Generate or Load Data
% Simulate customer purchasing data (e.g., annual spending in $ on categories).
rng(1); % For reproducibility
data = [randn(50, 2) * 50 + 200; % Cluster 1: High spenders
randn(50, 2) * 30 + 100; % Cluster 2: Mid-range spenders
randn(50, 2) * 20 + 50]; % Cluster 3: Budget spenders
categories = {'Food', 'Clothing'}; % Spending categories
% Step 2: Perform K-Means Clustering
numClusters = 3; % We know there are 3 customer segments
[idx, centroids] = kmeans(data, numClusters, 'Distance', 'sqeuclidean', 'Replicates', 5);
% Step 3: Visualize Clusters
figure;
gscatter(data(:,1), data(:,2), idx, 'rbg', 'osd');
hold on;
plot(centroids(:,1), centroids(:,2), 'kx', 'MarkerSize', 15, 'LineWidth', 3);
legend('Cluster 1', 'Cluster 2', 'Cluster 3', 'Centroids');
title('Customer Segmentation using K-Means Clustering');
xlabel(categories{1});
ylabel(categories{2});
grid on;
hold off;
% Step 4: Analyze Results
disp('Cluster Centroids (average spending per category):');
disp(centroids);
Explanation of the Code
- Data: We generate synthetic data simulating customers’ spending in two categories: Food and Clothing.
- Algorithm: K-Means is applied to group customers into three clusters based on their spending patterns.
- Distance Metric: The algorithm uses squared Euclidean distance to measure similarity.
- Visualization: The clusters are plotted with their centroids to show segmentation.
Output
The K-Means clustering will segment the data into three clusters. Below is an example of the centroids’ output:
Cluster Centroids (average spending per category): 200.5 201.8 105.3 102.7 50.2 49.8
The scatter plot will show customers grouped into clusters, with centroids marked by black crosses.
Example 5: Balancing a Cart-Pole System using Reinforcement Learning
Reinforcement Learning (RL) involves training agents to take actions in an environment to maximize a cumulative reward. In this example, we train an RL agent to balance a pole on a moving cart using MATLAB’s Reinforcement Learning Toolbox.
% Step 1: Define the Cart-Pole Environment
% Use MATLAB's built-in cart-pole environment
env = rlPredefinedEnv('CartPole-Continuous');
env.ResetFcn = @() 0; % Reset environment state
obsInfo = getObservationInfo(env);
actInfo = getActionInfo(env);
% Step 2: Define the RL Agent
% Create a Deep Q-Network (DQN) agent
statePath = [
featureInputLayer(obsInfo.Dimension(1),'Normalization','none','Name','state')
fullyConnectedLayer(24,'Name','fc1')
reluLayer('Name','relu1')
fullyConnectedLayer(24,'Name','fc2')
reluLayer('Name','relu2')
fullyConnectedLayer(numel(actInfo.Elements),'Name','fc3')
softmaxLayer('Name','output')];
criticOpts = rlRepresentationOptions('LearnRate',1e-3,'GradientThreshold',1);
critic = rlQValueRepresentation(statePath,obsInfo,actInfo,'ObservationInputNames','state','Options',criticOpts);
agentOpts = rlDQNAgentOptions( ...
'UseDoubleDQN',true, ...
'TargetUpdateFrequency',4, ...
'DiscountFactor',0.99, ...
'MiniBatchSize',64, ...
'ExperienceBufferLength',1e6);
agent = rlDQNAgent(critic,agentOpts);
% Step 3: Train the Agent
maxEpisodes = 500;
maxSteps = 500;
trainOpts = rlTrainingOptions( ...
'MaxEpisodes',maxEpisodes, ...
'MaxStepsPerEpisode',maxSteps, ...
'StopTrainingCriteria','AverageReward', ...
'StopTrainingValue',500, ...
'Verbose',false, ...
'Plots','training-progress');
% Start training
trainingStats = train(agent,env,trainOpts);
% Step 4: Evaluate the Trained Agent
simOpts = rlSimulationOptions('MaxSteps',500);
experience = sim(env,agent,simOpts);
Explanation of the Code
- Environment: The Cart-Pole environment simulates a cart that moves left or right to balance a pole vertically.
- Agent: We define a Deep Q-Network (DQN) agent with a neural network as its critic.
- Training: The agent learns to balance the pole by interacting with the environment and receiving rewards for balancing longer.
- Evaluation: After training, the agent is tested to evaluate its performance.
Output
After training, the training progress will display a graph showing the agent’s cumulative reward per episode. If training is successful, the average reward will meet the StopTrainingValue
criterion, indicating the agent has learned the task.
Training stopped. Average reward met the threshold of 500.
During evaluation, the trained agent will successfully balance the pole for the maximum allowed steps.
Example 6: Stock Price Prediction using LSTM in MATLAB
In this example, we use LSTM networks to predict future stock prices based on historical data. LSTM networks are well-suited for time-series data due to their ability to learn patterns across time.
% Step 1: Load Historical Stock Data
% Use MATLAB's inbuilt function to fetch financial data
ticker = 'AAPL'; % Apple stock
startDate = '01-Jan-2020';
endDate = '31-Dec-2022';
data = fetch(yahoo, ticker, 'Close', startDate, endDate);
stockPrices = data.Close;
% Normalize data for LSTM training
stockPricesNorm = (stockPrices - min(stockPrices)) / (max(stockPrices) - min(stockPrices));
% Step 2: Prepare Data for LSTM
% Create sequences for training
seqLen = 30; % Number of past days to use for prediction
X = [];
Y = [];
for i = 1:(length(stockPricesNorm) - seqLen)
X = [X; stockPricesNorm(i:i+seqLen-1)'];
Y = [Y; stockPricesNorm(i+seqLen)];
end
% Split into training and testing sets
trainRatio = 0.8;
splitIdx = floor(trainRatio * length(Y));
XTrain = X(1:splitIdx, :);
YTrain = Y(1:splitIdx);
XTest = X(splitIdx+1:end, :);
YTest = Y(splitIdx+1:end);
% Reshape data for LSTM input
XTrain = reshape(XTrain, [1, size(XTrain, 1), size(XTrain, 2)]);
XTest = reshape(XTest, [1, size(XTest, 1), size(XTest, 2)]);
% Step 3: Define the LSTM Network
layers = [
sequenceInputLayer(seqLen, 'Name', 'input')
lstmLayer(50, 'OutputMode', 'last', 'Name', 'lstm')
fullyConnectedLayer(1, 'Name', 'fc')
regressionLayer('Name', 'regression')];
% Set training options
options = trainingOptions('adam', ...
'MaxEpochs', 50, ...
'GradientThreshold', 1, ...
'InitialLearnRate', 0.01, ...
'MiniBatchSize', 20, ...
'Plots', 'training-progress', ...
'Verbose', false);
% Train the network
net = trainNetwork(XTrain, YTrain, layers, options);
% Step 4: Predict and Evaluate
YPred = predict(net, XTest);
YPredDenorm = YPred * (max(stockPrices) - min(stockPrices)) + min(stockPrices);
YTestDenorm = YTest * (max(stockPrices) - min(stockPrices)) + min(stockPrices);
% Plot actual vs predicted stock prices
figure;
plot(YTestDenorm, 'b', 'LineWidth', 1.5); hold on;
plot(YPredDenorm, 'r--', 'LineWidth', 1.5);
legend('Actual Prices', 'Predicted Prices');
xlabel('Time (Days)');
ylabel('Stock Price');
title('Actual vs Predicted Stock Prices');
Explanation of the Code
- Data Loading: The script fetches historical stock price data using MATLAB’s financial toolbox or external APIs like Yahoo Finance.
- Data Preparation: Historical prices are normalized and formatted into sequences suitable for LSTM input.
- LSTM Network: The network consists of a sequence input layer, an LSTM layer for learning time dependencies, and a regression output layer.
- Training: The model is trained using a subset of data, while another subset is used for evaluation.
- Evaluation: Predicted and actual prices are denormalized and compared graphically.
Output
The plot will show a comparison of actual and predicted stock prices. Ideally, the predicted line closely follows the actual prices:
Blue Line - Actual Stock Prices Red Dashed Line - Predicted Stock Prices
Example 7: Multimodal AI for Medical Diagnosis
This example demonstrates using multimodal AI to classify medical conditions by combining patient X-ray images and demographic/vital data in MATLAB.
% Step 1: Load Data
% Load the image data (e.g., X-rays)
imageFolder = 'MedicalImages'; % Folder with medical images
imageData = imageDatastore(imageFolder, 'IncludeSubfolders', true, 'LabelSource', 'foldernames');
% Load the tabular data (e.g., patient demographics and vital signs)
tabularData = readtable('PatientData.csv');
% Separate predictors and labels in the tabular data
tabularPredictors = tabularData(:, 1:end-1); % Exclude the label column
tabularLabels = tabularData.Label; % Extract the labels
% Step 2: Preprocess Data
% Preprocess image data: Resize images to a fixed size
inputSize = [224 224]; % Image size compatible with pretrained networks
augmentedImages = augmentedImageDatastore(inputSize, imageData);
% Normalize tabular data (e.g., scale numerical features to 0-1)
tabularPredictors = normalize(tabularPredictors);
% Convert categorical labels to numeric if necessary
tabularLabels = grp2idx(tabularLabels);
% Step 3: Define Networks for Each Modality
% Image modality: Use a pretrained convolutional neural network (e.g., ResNet50)
imageNetwork = resnet50;
imageLayers = [
imageInputLayer(inputSize, 'Name', 'image_input')
imageNetwork.Layers(2:end-3) % Extract layers except classification layers
fullyConnectedLayer(128, 'Name', 'fc_image')];
% Tabular modality: Define a simple feedforward neural network
tabularLayers = [
featureInputLayer(size(tabularPredictors, 2), 'Name', 'tabular_input')
fullyConnectedLayer(64, 'Name', 'fc_tabular')
reluLayer('Name', 'relu_tabular')];
% Combine modalities using concatenation
combinedLayers = [
concatenationLayer(1, 2, 'Name', 'concat')
fullyConnectedLayer(64, 'Name', 'fc_combined')
reluLayer('Name', 'relu_combined')
fullyConnectedLayer(2, 'Name', 'fc_output') % Binary classification
softmaxLayer('Name', 'softmax')
classificationLayer('Name', 'classification')];
% Create the multimodal network
lgraph = layerGraph;
lgraph = addLayers(lgraph, imageLayers);
lgraph = addLayers(lgraph, tabularLayers);
lgraph = addLayers(lgraph, combinedLayers);
lgraph = connectLayers(lgraph, 'fc_image', 'concat/in1');
lgraph = connectLayers(lgraph, 'fc_tabular', 'concat/in2');
% Step 4: Train the Network
% Specify training options
options = trainingOptions('adam', ...
'MaxEpochs', 20, ...
'MiniBatchSize', 32, ...
'Plots', 'training-progress', ...
'Verbose', false);
% Train the network
[trainedNet, info] = trainNetwork({augmentedImages, tabularPredictors}, tabularLabels, lgraph, options);
% Step 5: Evaluate the Model
% Test the trained network on new data
testImageData = imageDatastore('TestImages', 'IncludeSubfolders', true);
testTabularData = readtable('TestPatientData.csv');
testImages = augmentedImageDatastore(inputSize, testImageData);
testTabular = normalize(testTabularData(:, 1:end-1));
testLabels = testTabularData.Label;
predictions = classify(trainedNet, {testImages, testTabular});
% Calculate accuracy
accuracy = sum(predictions == testLabels) / numel(testLabels);
disp(['Test Accuracy: ', num2str(accuracy * 100), '%']);
Explanation of the Code
- Data Loading: Load image and tabular data. Images represent visual data, while tabular data contains numerical or categorical predictors.
- Preprocessing: Resize images to match the input size of the neural network and normalize tabular data for uniformity.
- Network Definition: Two separate subnetworks handle the modalities, which are then combined using a concatenation layer.
- Training: Train the combined multimodal network using both image and tabular data.
- Evaluation: Test the model on unseen data and calculate accuracy.
Example 8: AI for Games – AI Tic-Tac-Toe Using MATLAB
This example demonstrates how to implement an AI to play the game Tic-Tac-Toe using MATLAB. The AI employs the Minimax algorithm to make optimal decisions and play against a human player.
Game Description
The board is represented as a 3×3 matrix. The symbols are encoded as follows:
- 1: AI’s move (‘X’)
- -1: Player’s move (‘O’)
- 0: Empty cell
Code Implementation
% Initialize the Game Board
board = zeros(3, 3); % 3x3 matrix for Tic-Tac-Toe board
% Symbols: 1 for AI ('X'), -1 for player ('O'), 0 for empty
% Function to Display the Board
function displayBoard(board)
disp(strrep(strrep(num2str(board), '1', 'X'), '-1', 'O')); % Replace 1 with 'X' and -1 with 'O'
end
% Function to Check if There is a Winner
function winner = checkWinner(board)
winPatterns = [eye(3); flip(eye(3)); [1 1 1; 0 0 0; 0 0 0]; ...
[0 0 0; 1 1 1; 0 0 0]; [0 0 0; 0 0 0; 1 1 1]];
winner = 0; % No winner yet
for i = 1:size(winPatterns, 1)
if all(sum(winPatterns(i, :) .* board, 2) == 3) % AI wins
winner = 1;
return;
elseif all(sum(winPatterns(i, :) .* board, 2) == -3) % Player wins
winner = -1;
return;
end
end
end
% Function for Minimax Algorithm
function [score, move] = minimax(board, depth, isMaximizing)
winner = checkWinner(board);
if winner ~= 0 || depth == 0 || all(board(:) ~= 0) % End of game or depth limit
score = winner; % Return the winner as score
move = [];
return;
end
if isMaximizing
bestScore = -inf;
for i = 1:3
for j = 1:3
if board(i, j) == 0 % Empty cell
board(i, j) = 1; % AI's move
[newScore, ~] = minimax(board, depth - 1, false);
board(i, j) = 0; % Undo move
if newScore > bestScore
bestScore = newScore;
move = [i, j];
end
end
end
end
score = bestScore;
else
bestScore = inf;
for i = 1:3
for j = 1:3
if board(i, j) == 0 % Empty cell
board(i, j) = -1; % Player's move
[newScore, ~] = minimax(board, depth - 1, true);
board(i, j) = 0; % Undo move
if newScore < bestScore
bestScore = newScore;
move = [i, j];
end
end
end
end
score = bestScore;
end
end
% Main Game Loop
while true
displayBoard(board);
% Player's Turn
disp('Your move (row and column):');
[row, col] = deal(input('Row: '), input('Column: '));
if board(row, col) == 0
board(row, col) = -1;
else
disp('Invalid move. Try again.');
continue;
end
if checkWinner(board) ~= 0 || all(board(:) ~= 0)
break; % Game ends
end
% AI's Turn
disp('AI is thinking...');
[~, aiMove] = minimax(board, 5, true); % AI's depth of search = 5
board(aiMove(1), aiMove(2)) = 1;
if checkWinner(board) ~= 0 || all(board(:) ~= 0)
break; % Game ends
end
end
% Final Board State and Result
displayBoard(board);
winner = checkWinner(board);
if winner == 1
disp('AI wins!');
elseif winner == -1
disp('You win!');
else
disp('It''s a draw!');
end
Outputs
Sample outputs for various moves are displayed on the console, showing the updated board after each turn and the winner at the end.
Example 9: Visualizing AI and Machine Learning Models in MATLAB
Visualization plays a critical role in understanding and interpreting AI and Machine Learning models. MATLAB provides powerful tools to visualize data, model training progress, decision boundaries, and predictions.
Example: Visualizing Decision Boundaries of a Classification Model
In this example, we will train a Support Vector Machine (SVM) classifier to distinguish between two classes in a dataset and visualize the decision boundary.
Dataset Generation
% Generate synthetic data
rng(1); % Set seed for reproducibility
X = [randn(50, 2) + 2; randn(50, 2) - 2]; % Features
Y = [ones(50, 1); -ones(50, 1)]; % Labels: 1 for class 1, -1 for class 2
% Plot the data
figure;
gscatter(X(:, 1), X(:, 2), Y, 'rb', 'xo');
xlabel('Feature 1'); ylabel('Feature 2');
title('Synthetic Dataset');
legend('Class 1', 'Class 2');
The dataset consists of two clusters of points: one for Class 1 and another for Class 2.
Training the SVM Classifier
% Train an SVM classifier
SVMModel = fitcsvm(X, Y, 'KernelFunction', 'linear', 'Standardize', true);
% Predict the labels for the training data
predictedLabels = predict(SVMModel, X);
% Compute and display accuracy
accuracy = mean(predictedLabels == Y) * 100;
disp(['Training Accuracy: ', num2str(accuracy), '%']);
The linear kernel SVM is trained on the synthetic data, and the accuracy of the model is displayed.
Visualizing the Decision Boundary
% Visualize the decision boundary
d = 0.01; % Resolution of the grid
[x1Grid, x2Grid] = meshgrid(min(X(:, 1)):d:max(X(:, 1)), min(X(:, 2)):d:max(X(:, 2)));
gridPoints = [x1Grid(:), x2Grid(:)];
% Predict for each grid point
[gridLabels, scores] = predict(SVMModel, gridPoints);
% Plot the decision boundary and data points
figure;
gscatter(X(:, 1), X(:, 2), Y, 'rb', 'xo');
hold on;
contourf(x1Grid, x2Grid, reshape(gridLabels, size(x1Grid)), 1, 'LineColor', 'k', 'LineWidth', 1);
alpha(0.2); % Make the boundary semi-transparent
xlabel('Feature 1'); ylabel('Feature 2');
title('SVM Decision Boundary');
legend('Class 1', 'Class 2', 'Decision Boundary');
The decision boundary is displayed, separating the two classes in the feature space. Points on one side of the boundary belong to Class 1, while those on the other side belong to Class 2.
Feature Importance Visualization
% Visualize feature importance using weights
weights = SVMModel.Beta; % Linear SVM weights
figure;
bar(abs(weights));
xlabel('Feature');
ylabel('Weight Magnitude');
title('Feature Importance');
The bar plot displays the absolute values of the weights for each feature, indicating their relative importance in the classification decision.
Outputs
Sample outputs include:
- Dataset Plot: A scatter plot of the two classes in the dataset.
- Decision Boundary Plot: A visualization of the decision boundary separating the two classes.
- Feature Importance Plot: A bar chart showing the importance of each feature.
These visualizations provide insights into the model’s decision-making process and the underlying data structure.
Useful MATLAB Functions for AI
Practice Questions
Test Yourself
1. Fit a linear regression model to a dataset and visualize the results.
2. Train a simple neural network for binary classification.
3. Perform K-Means clustering on random 2D data and plot the clusters.
4. Experiment with different numbers of clusters (numClusters
) and observe how the segmentation changes. Try adding more spending categories to analyze multi-dimensional clustering.