Introduction: Everything in MATLAB is a Matrix
If there’s one fundamental truth you need to internalize about MATLAB, it’s this: everything in MATLAB is a matrix. This isn’t hyperbole or a marketing slogan—it’s a literal, architectural fact about how MATLAB represents and manipulates data. That scalar you just created? It’s a 1×1 matrix. That character string? It’s a 1×N matrix of characters. That seemingly “simple” variable x = 5? It’s stored in exactly the same way as a 1000×1000 matrix of doubles—as a contiguous block of memory organized in column-major order.
This matrix-centric design is what gives MATLAB its name (MATrix LABoratory), and it’s what makes MATLAB uniquely powerful for linear algebra, numerical analysis, and scientific computing. But it’s also the source of much confusion for beginners who try to think about MATLAB in the same way they think about C, Python, or other general-purpose languages.
In this comprehensive article, we’re going to dive deep into MATLAB’s matrix operations. We’ll explore how to create matrices, how to index into them, how to perform operations on them, and how MATLAB’s linear algebra engine works under the hood. By the time you finish this guide, you’ll have a profound understanding of why MATLAB is so effective for numerical computing, and you’ll be able to leverage its full power for your engineering and scientific applications.
The Fundamental Design Philosophy: Why Everything is a Matrix
To understand why MATLAB treats everything as a matrix, we need to go back to the 1970s, when Cleve Moler was developing the first version of MATLAB. Moler was a mathematician and computer scientist who needed a tool for teaching linear algebra and numerical analysis. The dominant languages at the time were Fortran and assembly, and performing even simple matrix operations required writing extensive boilerplate code.
Moler’s insight was that if you made matrices the fundamental data type—if you designed the entire language around matrix operations—you could create a language that was both concise and powerful for numerical computing. Instead of writing loops to add two vectors, you could simply type A + B. Instead of calling a Fortran subroutine to solve a linear system, you could type A \ b.
This design philosophy had profound implications:
- Syntax that mirrors mathematical notation: In linear algebra, we write Ax = b. In MATLAB, we type
A * x == b. The correspondence is nearly one-to-one. - Highly optimized linear algebra libraries: Because MATLAB knows that everything is a matrix, it can use highly optimized BLAS (Basic Linear Algebra Subprograms) and LAPACK routines under the hood. These are the gold standard for numerical linear algebra, hand-optimized by experts over decades.
- Natural vectorization: When your fundamental data type is a matrix, operations like
A .* B(element-wise multiplication) become natural and efficient. This enables “vectorized” code that avoids explicit loops—one of the keys to writing fast MATLAB code. - Unified memory model: Scalars, vectors, matrices, and higher-dimensional arrays are all represented the same way in memory. This simplifies the language implementation and makes it easier to write generic code that works with any size of data.
A Concrete Example: The Power of Matrix Thinking
Let me illustrate the power of MATLAB’s matrix-centric design with a concrete example. Suppose you have two vectors:
x = [1, 2, 3, 4, 5] y = [2, 3, 4, 5, 6]
And you want to compute their dot product: x · y = 1×2 + 2×3 + 3×4 + 4×5 + 5×6 = 70
In a language like C, you’d write:
int x[] = {1, 2, 3, 4, 5};
int y[] = {2, 3, 4, 5, 6};
int dot_product = 0;
for (int i = 0; i < 5; i++) {
dot_product += x[i] * y[i];
}
In MATLAB, you simply type:
x = [1, 2, 3, 4, 5];
y = [2, 3, 4, 5, 6];
dot_product = x * y' % x * y-transpose (inner product)
Or, even simpler:
dot_product = dot(x, y) % Using the built-in dot() function
But here’s where it gets really interesting. Suppose you have not just two vectors, but 1000 vectors, and you want to compute the dot product of each pair. In C, you’d need nested loops. In MATLAB, you just use matrix multiplication:
% X is a 1000×5 matrix, where each row is a vector
X = rand(1000, 5);
% The dot products of all pairs are given by X * X'
dot_products = X * X';
This isn’t just more concise—it’s dramatically faster, because MATLAB calls optimized BLAS routines for the matrix multiplication, which can use multiple CPU cores and SIMD instructions.
This example illustrates a fundamental principle of effective MATLAB programming: whenever possible, express your computation in terms of matrix operations. Your code will be shorter, clearer, and faster.



