Kahibaro
Discord Login Register

Basic Linear Algebra with MATLAB

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

matlab
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:

  1. 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.
  2. 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 example x = A\b. You will later see why this operator is preferred over computing an explicit inverse.
  3. Performing matrix operations such as addition, subtraction, and multiplication when dimensions are compatible. For example, if A is $m \times n$ and B is $n \times p$, then A*B is a valid matrix product that produces an $m \times p$ result.
  4. Working with fundamental matrix properties such as the determinant det(A), the rank rank(A), or the inverse inv(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.
  5. Analyzing transformations using eigenvalues and eigenvectors with the eig function. 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.
  6. 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

matlab
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

matlab
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

matlab
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: 4

Comments

Please login to add a comment.

Don't have an account? Register now!