Table of Contents
Overview
This chapter introduces vectors, matrices, and arrays as MATLAB’s core way of storing and working with data. Every number you create in MATLAB belongs to some kind of array, even if it looks like a single value. Understanding this idea is essential, because almost everything you do in MATLAB relies on array operations.
You will later explore specific aspects such as row and column vectors, matrix creation, indexing, reshaping, and special matrices in dedicated subchapters. Here the goal is to build a conceptual foundation, show how MATLAB thinks about data as arrays, and explain how this affects your everyday work.
MATLAB’s Fundamental Data Container
In MATLAB, the fundamental data container is the array. An array is a collection of values arranged in one or more dimensions. Even a single scalar value like 5 is technically a $1 \times 1$ array.
When you write something as simple as:
a = 10;
MATLAB stores a as a numeric array with one row and one column. If you then write:
b = [1 2 3];you now have a row of three numbers. That is a $1 \times 3$ array. If you create:
C = [1 2; 3 4];you have a $2 \times 2$ array, which you typically call a matrix. All these are numeric arrays that differ only in their size and shape, not in the basic kind of container.
This uniform idea allows MATLAB to apply the same functions to data of many different shapes. For example, size, length, sum, and similar functions work in predictable ways on any array, regardless of its dimensions.
Dimensions, Size, and Shape
An array in MATLAB is described by its dimensions. For a numeric array, you can think of its shape as:
$$
\text{rows} \times \text{columns} \times \text{page}_3 \times \text{page}_4 \times \dots
$$
The simplest and most common cases are:
- Scalars, which have size $1 \times 1$.
- Vectors, which are $1 \times n$ or $n \times 1$.
- Matrices, which are $m \times n$ with both $m$ and $n$ greater than or equal to 1.
- Higher dimensional arrays, which have more than two dimensions, such as $m \times n \times p$ and beyond.
You can ask MATLAB for the size of an array with the size function, and this will tell you its dimensions. You will learn how to inspect and manipulate these dimensions in later subchapters, but it is important already to be conscious of the idea of array shape.
The shape of an array strongly influences how operations behave. For example, when you add two arrays, MATLAB checks if their shapes are compatible. When you multiply matrices with the matrix multiplication operator, MATLAB uses the rules of linear algebra, which depend on the number of rows and columns. These details will be addressed where matrix operations are discussed, but they are all based on the simple idea of array dimensions.
Vectors and Matrices as Special Cases of Arrays
Vectors and matrices are special cases of more general arrays, but they are so common in MATLAB that they are often named separately.
A vector is an array with a single non singleton dimension. If it has one row and multiple columns, you call it a row vector. If it has one column and multiple rows, you call it a column vector. Conceptually, vectors represent ordered collections of numbers, such as time samples, indices, or parameter values.
A matrix is a two dimensional array, with both rows and columns. In many engineering, scientific, and mathematical problems, matrices represent systems of equations, transformations, images in grayscale form, or tabular numeric data.
Higher dimensional arrays extend this idea to three dimensions or more. For example, a color image can be represented as a three dimensional array where the third dimension indexes color channels, or a sequence of images can be stacked along a third dimension.
Although you will work most often with vectors and matrices, it is helpful to see them as part of the same general concept, because MATLAB does not really treat them as fundamentally different objects.
Homogeneous Numeric Content
By default, a numeric array in MATLAB stores elements of the same numeric type, for example double. This is known as a homogeneous array. When you write:
A = [1 2 3; 4 5 6];
each element of A is a number of the same underlying type. MATLAB does not allow different types in different elements of a standard numeric array. If you need to mix types and other structures, such as text and numbers in one container, you use other storage forms like cell arrays or tables, which are covered elsewhere.
This uniformity allows MATLAB to perform fast operations across entire arrays and is one reason why MATLAB is efficient for numerical computing. Many functions in MATLAB are written to operate on a whole array at once. When you understand that arrays are homogeneous, you also understand why operations like A + 1 make sense and apply element by element.
Linear Indexing and Memory Layout Concept
Internally, MATLAB stores arrays in a single contiguous block of memory, and it uses column major order. You do not need to manage memory, but it is useful conceptually to know that MATLAB reads and writes arrays down columns first, then across rows.
Every element in an array has a subscript position, such as $(i, j)$ in a matrix. MATLAB also assigns each element a single linear index that counts element positions along columns. Although you will learn the details of indexing later, it is important to recognize already that MATLAB can address elements either by their row and column, or by a single integer index that runs through all entries.
This idea influences how some functions behave. For example, some functions treat an array as a long vector when they operate in their simplest form, and only when you specify a dimension do they focus on rows or columns. The concept that everything underneath is laid out in one long column is tied to the general theme that arrays are central and systematically organized.
Working with Whole Arrays at Once
A key MATLAB habit is to work with whole arrays rather than individual elements whenever you can. If you have a vector of 100 values, you usually avoid writing 100 separate commands. Instead, you apply a function or operator to the entire vector in one go.
For example, if x is a vector of measurements, and you want to convert them to a different unit, you typically write an expression that multiplies or adds constants to x as a whole. MATLAB will handle this element by element. The idea that an operation is applied across an entire array underlies vectorized programming, which is an important MATLAB style that you will use more as your programs become larger.
Matrices and higher dimensional arrays follow the same idea. You use operators and functions to transform whole blocks of data. This gives MATLAB its expressive power and helps you write concise, readable code.
Arrays as the Default Output of Functions
Most MATLAB functions return arrays as their outputs. A function that reads data from a file usually returns an array of numbers or a combination of arrays inside a higher level container. A function that computes something like a sine or cosine will normally accept arrays and return arrays of the same shape.
Even when functions return what look like single values, they are in fact arrays with appropriate sizes. For instance, the determinant of a matrix is a scalar, which, in MATLAB terms, is a $1 \times 1$ array.
This consistency means that you can combine function calls with operators to build expressions that transform arrays step by step. You connect outputs and inputs almost always through arrays.
Arrays Across Different Data Types
Although numeric arrays are the most common, the array concept in MATLAB is not limited to numbers. Strings, logical values, and other data can also form arrays with dimensions and shapes. Each of these will be covered in their own chapters, but the unifying idea is that MATLAB tries to arrange things into arrays whenever possible.
When you start dealing with tables, structures, and cell arrays, you will see variations on the theme of collections, but there the elements can differ in type or structure. For classic numeric computing, however, you will mostly think about standard homogeneous arrays.
Arrays and Performance for Beginners
For a beginner, it is enough to know that MATLAB is designed to work efficiently with arrays. When you operate on whole arrays rather than on single elements in a loop, you often get better performance and simpler code.
This performance characteristic reinforces why arrays are central. The language and built in libraries are optimized to handle array based calculations. Later, when you learn about performance tips and vectorization, you will exploit this design to write faster code without needing to manage low level details.
How Arrays Connect the Course Topics
Many of the later topics in this course, such as linear algebra, plotting, data analysis, and even image handling, all rely on the understanding that data lives in arrays. A plot of a signal typically uses two vectors, one for horizontal values and one for vertical values. A system of linear equations becomes a matrix equation. A grayscale image becomes a two dimensional array of intensities.
As you move forward into specific subchapters, you will learn how to create different shaped arrays, access and modify their entries, and apply mathematical operations that respect their structure. Those details will deepen the conceptual picture introduced here.
Key ideas to remember:
Arrays are the fundamental way MATLAB stores data, and even scalars, vectors, and matrices are all arrays with different sizes and shapes. Arrays are homogeneous collections of values arranged in one or more dimensions, and MATLAB’s functions and operators are designed to work on whole arrays at once. Understanding array size, shape, and the idea that most MATLAB outputs are arrays will help you make sense of almost every topic that follows.