Matrix Algebra in Matlab Programming

MATLAB Illustration

Introduction

rn

Matrix algebra is a fundamental part of mathematics and engineering, used extensively in linear systems, control engineering, computer graphics, and scientific computing. MATLAB (MATrix LABoratory) is specifically designed for matrix-based operations, making it the ideal tool for performing linear algebra efficiently.

rn

This blog covers the essential matrix operations in MATLAB with examples.

rn
rn

Step 1: Define Matrices

rn

Define matrices in MATLAB using square brackets []:

rn
rn
rn
rn
 
rn
rn
rn
A = [1 2; 3 4]; % 2x2 matrixrnB = [5 6; 7 8]; % 2x2 matrixrn
rn
rn

For larger matrices:

rn
rn
rn
rn
 
rn
rn
rn
C = [1 2 3; 4 5 6; 7 8 9]; % 3x3 matrixrn
rn
rn
rn

Step 2: Matrix Addition and Subtraction

rn

Add or subtract matrices of the same size:

rn
rn
rn
rn
 
rn
rn
rn
D = A + B;rnE = B - A;rndisp(D)rndisp(E)rn
rn
rn

Output:

rn
rn
rn
rn
 
rn
rn
rn
D = [6 8; 10 12]rnE = [4 4; 4 4]rn
rn
rn
rn

Step 3: Matrix Multiplication

rn

Element-wise multiplication: Use .* operator

rn
rn
rn
rn
 
rn
rn
rn
F = A .* B;rndisp(F)rn
rn
rn

Matrix multiplication: Use * operator

rn
rn
rn
rn
 
rn
rn
rn
G = A * B;rndisp(G)rn
rn
rn

Note: For matrix multiplication, the inner dimensions must match.

rn
rn

Step 4: Transpose of a Matrix

rn

Transpose a matrix using ':

rn
rn
rn
rn
 
rn
rn
rn
A_T = A';rndisp(A_T)rn
rn
rn
rn

Step 5: Determinant and Inverse

rn

Compute the determinant using det() and inverse using inv():

rn
rn
rn
rn
 
rn
rn
rn
det_A = det(A);rninv_A = inv(A);rndisp(det_A)rndisp(inv_A)rn
rn
rn

Note: Only square matrices with non-zero determinant have inverses.

rn
rn

Step 6: Identity and Zero Matrices

rn

Create identity or zero matrices:

rn
rn
rn
rn
 
rn
rn
rn
I = eye(3); % 3x3 Identity matrixrnZ = zeros(2,3); % 2x3 Zero matrixrndisp(I)rndisp(Z)rn
rn
rn
rn

Step 7: Eigenvalues and Eigenvectors

rn

Compute eigenvalues and eigenvectors using eig():

rn
rn
rn
rn
 
rn
rn
rn
[V, D] = eig(A); % V = eigenvectors, D = eigenvalues diagonal matrixrndisp(V)rndisp(D)rn
rn
rn

Eigenvalues are used in stability analysis, vibration analysis, and system modeling.

rn
rn

Step 8: Solving Linear Systems

rn

Solve linear equations Ax = b using MATLAB:

rn
rn
rn
rn
 
rn
rn
rn
b = [5; 11];rnx = A b; % Solves Ax = brndisp(x)rn
rn
rn

This is more efficient and accurate than calculating inv(A)*b.

rn
rn

Applications of Matrix Algebra in MATLAB

rn
    rn
  • rn

    Solving systems of linear equations

    rn
  • rn
  • rn

    Engineering simulations and control system design

    rn
  • rn
  • rn

    Computer graphics transformations

    rn
  • rn
  • rn

    Data analysis and machine learning computations

    rn
  • rn
  • rn

    Scientific and mathematical modeling

    rn
  • rn
rn
rn

Conclusion

rn

Matrix algebra in MATLAB is powerful, efficient, and versatile. From basic operations like addition and multiplication to advanced calculations like eigenvalues and solving linear systems, MATLAB provides all the tools needed for linear algebra applications in engineering, science, and data analysis.

rn

By mastering these matrix operations, you can perform complex computations efficiently and analyze real-world problems with confidence.

Verified Feedback

What Engineering Students Say

Real feedback from students across top engineering universities worldwide.

Verified Student

“I got full marks on my MATLAB DSP assignment! The filter design code was completely vectorized, the frequency response plots were exact, and the delivery was 8 hours before my deadline. Highly recommended!”

AS

Aditi Sharma

IIT Bombay • Signal Processing Coursework
Verified Student

“Our Simulink EV powertrain model had severe algebraic loop and solver errors. The MATLABSolutions team fixed the solver configuration in 4 hours and provided an annotated scope diagram. Lifesaver for my final year!”

JM

John M.

Monash University, Australia • Simulink Dynamic Model
Technical Knowledge Base

Latest MATLAB Guides & Tutorials

Explore deep-dive technical articles written by our engineering team to master complex MATLAB & Simulink topics.

MATLAB Guide 5 Min Read

Reinforcement Learning for Microgrid Energy Management in MATLAB & Simulink

Operating a modern microgrid is an ongoing balancing act. Between changing solar output, shifting wind speeds, volatile electricity pricing, and unpredictable consumer demand, a...

MATLAB Guide 5 Min Read

Physics-Informed Neural Networks (PINNs) for Microgrid Dynamics and Power Flow in MATLAB

Modern microgrids operate with low physical inertia, rapid inverter switching dynamics, and intermittent renewable power generation. Simulating these systems requir...