15. Basic Linear Algebra with MATLAB
Table of Contents
Overview
Linear algebra is at the heart of MATLAB. In fact, the name MATLAB comes from “MATrix LABoratory”. Many operations that you will later use for data analysis, simulation, and modeling are built on vector and matrix computations. This chapter gives you a practical first view of what “linear algebra with MATLAB” means, and how MATLAB is designed to make these operations both natural and efficient.
You will see how vectors and matrices are the basic data structures, how common mathematical operations correspond to simple MATLAB commands, and why using MATLAB’s built in linear algebra functions is usually preferable to writing your own loops. The detailed techniques for norms, matrix multiplication, solving systems, eigenvalues, and safe practices will each appear in their own sections later. Here you focus on the overall picture and mindset.
MATLAB as a Matrix Language
In MATLAB, virtually every numeric variable is stored as an array. Scalars are treated as $1 \times 1$ arrays, vectors as $1 \times n$ or $n \times 1$ arrays, and matrices as $m \times n$ arrays. This matches very closely the objects you encounter in a standard linear algebra course.
When you enter something like
A = [1 2 3; 4 5 6];
x = [7; 8; 9];
you are already working in a linear algebra world. A is a $2 \times 3$ matrix and x is a $3 \times 1$ column vector. Many of MATLAB’s basic operators, such as *, +, and \, are defined in terms of matrix algebra, not just elementwise arithmetic. As a result, operations that might require several nested loops in another language often reduce to a single, readable statement in MATLAB.
Typical Linear Algebra Tasks in MATLAB
In practical applications, you will encounter a recurring set of linear algebra tasks. MATLAB provides built in implementations for all of them, optimized in low level libraries such as BLAS and LAPACK, so you can focus on the mathematical formulation rather than numerical details.
Common tasks include:
- Forming and manipulating vectors and matrices, such as building coefficient matrices for systems of equations, assembling data into rectangular arrays, and extracting rows or columns that represent specific variables or observations.
- Solving linear systems of equations of the form $A x = b$, where $A$ is a matrix of coefficients and $x$ is a vector of unknowns. In MATLAB this is expressed very compactly using the backslash operator
\, for examplex = A\b. You will later see why this operator is preferred over computing an explicit inverse. - Performing matrix operations such as addition, subtraction, and multiplication when dimensions are compatible. For example, if
Ais $m \times n$ andBis $n \times p$, thenA*Bis a valid matrix product that produces an $m \times p$ result. - Working with fundamental matrix properties such as the determinant
det(A), the rankrank(A), or the inverseinv(A)when it exists. These quantities are useful for understanding whether a system has a unique solution, whether a matrix is singular, and how sensitive computations may be. - Analyzing transformations using eigenvalues and eigenvectors with the
eigfunction. For a square matrix $A$, eigenvalues and eigenvectors give insight into stability, repeated application of linear transformations, and decompositions that appear in methods such as principal component analysis. - Measuring sizes and lengths using vector and matrix norms. Norms capture the idea of magnitude or length, such as the Euclidean length of a vector, and are important when discussing errors and convergence.
Each of these topics will later receive its own focused section. What is important here is to see them as parts of one coherent toolbox of linear algebra operations that MATLAB supports very directly.
Thinking in Terms of Array Operations
Because MATLAB is designed for matrix computations, it encourages a style of programming that operates on entire arrays at once. This is often called vectorization. Instead of writing explicit loops over indices, you typically write one statement that represents the whole mathematical operation.
For example, if you have a matrix $A$ and you want to multiply every column by a given scalar alpha, you can simply write
B = alpha * A;Mathematically this is $B = \alpha A$. Similarly, if you have a matrix $A$ and a column vector $x$ and you want to compute $A x$, in MATLAB you write
y = A * x;The shape of the operands, that is their number of rows and columns, determines whether an operation is valid. If the inner dimensions of a matrix product do not match, MATLAB reports an error. Learning to think in terms of array dimensions and linear algebra rules will make your MATLAB code more robust and easier to reason about.
Elementwise operations, such as multiplying corresponding entries of two matrices, are also supported but use different operators like .* and .^. Their main use, and their contrast with true matrix operations, are treated separately. For linear algebra, the non dotted versions are the important ones because they correspond directly to mathematical definitions.
Expressing Linear Systems and Transformations
One of the strengths of MATLAB is how naturally you can translate mathematical models into code. Many models, from simple regression to physical simulations, can be expressed as systems of linear equations or as linear transformations.
Suppose you have a system
$$
\begin{aligned}
2x + y &= 5, \\
x - 3y &= -4.
\end{aligned}
$$
You can write this in matrix form as $A x = b$ with
$$
A = \begin{bmatrix} 2 & 1 \\ 1 & -3 \end{bmatrix},
\quad
x = \begin{bmatrix} x \\ y \end{bmatrix},
\quad
b = \begin{bmatrix} 5 \\ -4 \end{bmatrix}.
$$
In MATLAB this becomes
A = [2 1; 1 -3];
b = [5; -4];
x = A \ b;
The single statement x = A\b; expresses “solve the linear system $A x = b$ for the unknown vector $x$”. This pattern occurs in many contexts, including curve fitting and discretized differential equations.
Similarly, linear transformations that act on vectors can be modeled by matrix multiplication. If you think of each column of a matrix as the image of a basis vector under a transformation, then multiplying by that matrix applies the transformation to any input vector. When you write y = A*x; you are not just performing arithmetic, you are applying a linear mapping encoded by A. Later on, eigenvalues and eigenvectors give a finer description of how this mapping behaves along particular directions.
Built In Linear Algebra Functions
MATLAB provides a large set of specialized functions for linear algebra, many of which are wrappers around robust, tested numerical libraries. For a beginner, the most relevant categories are:
Functions for basic matrix properties, such as size, length, det, rank, and trace. These tell you the dimensions, determinant, rank, and trace of matrices, which are essential for diagnosing singularity and understanding the structure of a matrix.
Functions for solving systems and factorizations, such as the backslash operator \, the forward slash / for right division, and decomposition functions like lu, qr, and chol. While you may not need factorizations immediately, it is useful to know that MATLAB can expose them if needed.
Functions for orthogonality and symmetric matrices, such as orth for an orthonormal basis of the column space and eig and svd for eigenvalue and singular value decompositions. These are central to many advanced algorithms, but even at a beginner level you will at least see how to call them and interpret basic output.
Functions for norms and conditioning, such as norm for vector and matrix norms and cond for the condition number of a matrix. These provide measures of sensitivity and error magnification in computations.
Even if you do not remember every function name at first, the key idea is that MATLAB has a ready made tool for most linear algebra operations you might need. You normally do not implement algorithms like Gaussian elimination yourself. Instead, you formulate your problem in terms of matrices and vectors, then call the appropriate built in function.
Numerical Considerations
Although the mathematical concepts of linear algebra are exact, computations on a computer are approximate. MATLAB uses floating point arithmetic, which introduces small rounding errors. For many everyday tasks this is not a practical issue, but in linear algebra it can influence decisions about which operations to apply.
For example, in exact mathematics, if $A$ is invertible, you might write $x = A^{-1} b$ to solve a linear system. In MATLAB you could compute inv(A)*b, but this is usually discouraged for numerical and efficiency reasons. The expression A\b calls algorithms that are generally more accurate and faster than computing an explicit inverse.
Similarly, some matrices are ill conditioned. This means small changes in input can cause large changes in the solution. The condition number, available through the function cond, measures this sensitivity. When you later work with cond and norms, you will see how MATLAB helps detect potentially unstable problems and supports safer strategies.
As a beginner, the main lesson is to prefer MATLAB’s high level linear algebra operations, such as \, over manually piecing together low level steps. Doing so usually gives you better numerical behavior “for free”.
How Linear Algebra Appears in Applications
Once you are familiar with the basic operations, you will start to see linear algebra hidden behind many common MATLAB tasks, even if it is not presented explicitly.
Data analysis frequently organizes measurements into matrices where each row is an observation and each column a variable. Operations that combine variables, project data into new coordinates, or fit linear models can often be expressed using matrix multiplication and linear systems.
Signal and image processing use matrices to represent filters and transformations. For instance, a small matrix can represent a convolution kernel that is applied across an image, and eigenvalue computations can appear in tasks like dimensionality reduction.
Numerical solutions of differential equations often discretize continuous systems into linear algebra problems. At each step of a time marching algorithm, you may need to solve a system that looks like $A x = b$ with different right hand sides.
Optimization problems, even if originally nonlinear, are commonly solved by iteratively solving linear approximations. Each iteration uses a linear algebra core, often involving factors of matrices and solution of systems.
MATLAB’s design reflects all these uses. Whenever you see arrays, multiplications, and divisions with matrices and vectors, you are most likely doing linear algebra, even if you only think of it as “manipulating data”.
Relationship to Later Topics in This Course
The remaining sections of this chapter will each address specific building blocks of linear algebra in MATLAB.
You will first work with vectors and norms to gain an intuitive sense of length and distance. Then you will revisit matrix addition and multiplication, this time from a linear algebra perspective rather than as mere array operations. Solving systems using the backslash operator will show you how to handle equations efficiently. Functions like inv, det, and rank will be introduced with care, alongside safer patterns that avoid numerical pitfalls. Eigenvalues and eigenvectors will give a window into the internal structure of linear transformations, followed by broader advice on using MATLAB’s linear algebra functions in a reliable way.
As you go through those topics, keep in mind that they all fit into the same picture introduced here: MATLAB treats vectors and matrices as first class objects, and most of its power comes from applying linear algebra operations to them in a direct and expressive way.
Key ideas to remember:
MATLAB is built around vectors and matrices, and many operators implement true linear algebra, not just elementwise arithmetic.
You typically express mathematical problems in matrix form, then solve them using high level commands like A\b, eig(A), or norm(x).
Rely on MATLAB’s built in linear algebra functions instead of writing your own low level algorithms. This is usually more accurate, faster, and easier to read.
Be aware that computations are approximate. Prefer numerically stable patterns, such as using \ instead of inv(A)*b, especially when solving systems of equations.
Views: 0
Where to Move
Move chapter:
- ☰ 1. Getting Started with MATLAB
- ☰ 1.1. What Is MATLAB and What Is It Used For
- ☰ 1.2. Installing MATLAB and Setting Up the Environment
- ☰ 1.3. Understanding the MATLAB Desktop Interface
- ☰ 1.4. Command Window, Workspace, and Current Folder
- ☰ 1.5. Basic Workflow and Running Simple Commands
- ☰ 1.6. Getting Help and Documentation
-
- ☰ 2. MATLAB Language Basics
- ☰ 2.1. MATLAB Syntax and Expressions
- ☰ 2.2. Variables and Assignment
- ☰ 2.3. Data Types and Type Conversion
- ☰ 2.4. Vectors and Matrices Fundamentals
- ☰ 2.5. Creating and Indexing Arrays
- ☰ 2.6. Basic Arithmetic and Elementwise Operations
- ☰ 2.7. Using Built-In Constants and Functions
-
- ☰ 3. Working with Variables and the Workspace
- ☰ 3.1. Creating, Viewing, and Editing Variables
- ☰ 3.2. Workspace Browser and Variable Inspector
- ☰ 3.3. Saving and Loading Data Files
- ☰ 3.4. MAT-Files and Importing External Data
- ☰ 3.5. Clearing Variables and Managing the Workspace
-
- ☰ 4. Vectors, Matrices, and Arrays
- ☰ 4.1. Row and Column Vectors
- ☰ 4.2. Matrix Creation and Concatenation
- ☰ 4.3. Indexing, Slicing, and Logical Indexing
- ☰ 4.4. Matrix Operations vs Elementwise Operations
- ☰ 4.5. Reshaping and Rearranging Arrays
- ☰ 4.6. Special Matrices and Functions
-
- ☰ 5. Basic Programming Constructs
- ☰ 5.1. Scripts vs Functions
- ☰ 5.2. Control Flow with if, elseif, else
- ☰ 5.3. Loops with for and while
- ☰ 5.4. break, continue, and return
- ☰ 5.5. Logical Expressions and Relational Operators
- ☰ 5.6. Best Practices for Clear and Readable Code
-
- ☰ 6. Functions in MATLAB
- ☰ 6.1. Creating Simple Functions
- ☰ 6.2. Function Inputs and Outputs
- ☰ 6.3. Function Files and the Path
- ☰ 6.4. Local vs Global Variables
- ☰ 6.5. Anonymous Functions
- ☰ 6.6. Organizing Code into Multiple Functions
-
- ☰ 7. Working with Numbers and Math
- ☰ 7.1. Scalars and Vectorized Computations
- ☰ 7.2. Basic Mathematical Functions
- ☰ 7.3. Trigonometric and Exponential Functions
- ☰ 7.4. Complex Numbers in MATLAB
- ☰ 7.5. Rounding, Modulo, and Special Values
- ☰ 7.6. Numerical Precision and Floating Point Considerations
-
- ☰ 8. Strings, Characters, and Categorical Data
- ☰ 8.1. Character Arrays and String Arrays
- ☰ 8.2. Creating and Manipulating Strings
- ☰ 8.3. Converting Between Data Types and Strings
- ☰ 8.4. Comparing and Searching Strings
- ☰ 8.5. Categorical Arrays Basics
-
- ☰ 9. Tables, Structures, and Cell Arrays
- ☰ 9.1. Introduction to Tables
- ☰ 9.2. Creating Tables and Accessing Data
- ☰ 9.3. Modifying and Joining Tables
- ☰ 9.4. Structures and Nested Structures
- ☰ 9.5. Cell Arrays and Cell Indexing
- ☰ 9.6. Choosing the Right Container Type
-
- ☰ 10. Importing and Exporting Data
- ☰ 10.1. Reading Text and CSV Files
- ☰ 10.2. Importing Excel Spreadsheets
- ☰ 10.3. Importing Data with the Import Tool
- ☰ 10.4. Exporting Data to Files
- ☰ 10.5. Working with Image and Audio Files
- ☰ 10.6. Basic File Management from MATLAB
-
- ☰ 11. Plotting and Visualization
- ☰ 11.1. Introduction to 2D Plotting
- ☰ 11.2. Customizing Plots and Axes
- ☰ 11.3. Multiple Plots in One Figure
- ☰ 11.4. Labels, Titles, Legends, and Annotations
- ☰ 11.5. Plotting with Different Line and Marker Styles
- ☰ 11.6. Saving and Exporting Figures
-
- ☰ 12. Advanced Plotting for Beginners
- ☰ 12.1. Subplots and Tiled Layouts
- ☰ 12.2. Bar, Scatter, and Stem Plots
- ☰ 12.3. Histograms and Density Plots
- ☰ 12.4. 3D Plots and Surface Plots
- ☰ 12.5. Using plottools and Interactive Editing
- ☰ 12.6. Basics of Handle Graphics
-
- ☰ 13. Working with Scripts and Live Scripts
- ☰ 13.1. Creating and Running Script Files
- ☰ 13.2. Sectioning Code and Running Sections
- ☰ 13.3. Live Scripts and Rich Text
- ☰ 13.4. Adding Equations, Images, and Outputs
- ☰ 13.5. Sharing and Exporting Live Scripts
-
- ☰ 14. Debugging and Code Quality
- ☰ 14.1. Finding and Fixing Syntax Errors
- ☰ 14.2. Setting Breakpoints and Stepping Through Code
- ☰ 14.3. Inspecting Variable Values During Execution
- ☰ 14.4. Using the Editor Debugging Tools
- ☰ 14.5. Using Code Analyzer and Warnings
- ☰ 14.6. Writing Clean and Maintainable MATLAB Code
-
- ☰ 15. Basic Linear Algebra with MATLAB
- ☰ 15.1. Vectors and Norms
- ☰ 15.2. Matrix Addition and Multiplication
- ☰ 15.3. Solving Linear Systems with Backslash
- ☰ 15.4. Inverses, Determinants, and Rank
- ☰ 15.5. Eigenvalues and Eigenvectors
- ☰ 15.6. Using Linear Algebra Functions Safely
-
- ☰ 16. Data Analysis and Statistics Fundamentals
- ☰ 16.1. Descriptive Statistics and Summary Measures
- ☰ 16.2. Sorting, Filtering, and Grouping Data
- ☰ 16.3. Basic Correlation and Regression
- ☰ 16.4. Simple Data Cleaning Techniques
- ☰ 16.5. Basic Time Series Handling
-
- ☰ 17. Working with Time and Dates
- ☰ 17.1. Representing Dates and Times
- ☰ 17.2. Creating and Formatting datetime Arrays
- ☰ 17.3. Date Calculations and Differences
- ☰ 17.4. Plotting Data over Time
-
- ☰ 18. Introduction to Signal and Image Handling
- ☰ 18.1. Basic Signal Representation in MATLAB
- ☰ 18.2. Simple Filtering and Transformations
- ☰ 18.3. Reading, Displaying, and Writing Images
- ☰ 18.4. Basic Image Transformations
-
- ☰ 19. Automation and Scripting Tasks
- ☰ 19.1. Batch Processing of Files
- ☰ 19.2. Writing Reusable Utility Scripts
- ☰ 19.3. Using Relative Paths and the MATLAB Path
- ☰ 19.4. Simple User Interaction with input and disp
-
- ☰ 20. Introduction to Toolboxes
- ☰ 20.1. What Are Toolboxes and How to Use Them
- ☰ 20.2. Exploring Installed Toolboxes
- ☰ 20.3. Finding Functions in Toolboxes
- ☰ 20.4. Add-Ons and MATLAB File Exchange
-
- ☰ 21. Best Practices and Next Steps
- ☰ 21.1. Organizing Projects and Folders
- ☰ 21.2. Version Control Basics with MATLAB
- ☰ 21.3. Performance Tips for Beginners
- ☰ 21.4. Learning Resources and Documentation Path
- ☰ 21.5. Suggested Practice Projects for Beginners
-
Comments
Please login to add a comment.
Don't have an account? Register now!