Kahibaro
Discord Login Register

Vectors and Matrices Fundamentals

Why Vectors and Matrices Matter in MATLAB

MATLAB is built around vectors and matrices. Almost everything you do in MATLAB uses them in some way, even if you do not notice it at first. Understanding what vectors and matrices are, and how MATLAB represents them, is essential before you start creating and indexing arrays or doing more advanced operations.

A scalar is a single number, for example 5 or 3.14. A vector is a one dimensional collection of numbers arranged in a row or in a column. A matrix is a rectangular grid of numbers with rows and columns. In MATLAB, both vectors and matrices are stored as arrays of numbers, and they follow the same basic rules.

When you write a number in MATLAB without any brackets, such as x = 7;, you are working with a scalar. As soon as you start grouping several numbers together with brackets, MATLAB treats the result as a vector or a matrix. This is where the language becomes powerful, because many operations can be applied to all elements of a vector or matrix at once.

Row Vectors and Column Vectors as 1D Arrays

The simplest non scalar arrays in MATLAB are row vectors and column vectors. A row vector contains elements arranged horizontally. For example, in the Command Window you can write

matlab
v = [1 2 3 4];

Here v is a row vector with 4 elements. You can also separate elements with commas, so v = [1, 2, 3, 4]; creates the same row vector. MATLAB internally stores the elements in a fixed order, and you can access each element by its position, but detailed indexing is covered in a later chapter.

A column vector contains elements arranged vertically. To create a column vector, you separate elements with semicolons inside the brackets:

matlab
w = [1; 2; 3; 4];

Now w is a column vector with 4 rows and 1 column. The same numbers as in v are now written as a column. Conceptually, MATLAB treats row vectors and column vectors differently in linear algebra operations, so it is important to recognize the shapes. In everyday use, you often create row vectors for parameters or simple sequences, and column vectors for data that looks like a single measurement over several observations.

Although both row and column vectors contain one dimension that is equal to 1, MATLAB still keeps track of whether the 1 is in the number of rows or the number of columns. You can check this with the size function, which returns the number of rows and columns:

matlab
size(v)   % gives 1   4
size(w)   % gives 4   1

The result tells you that v has 1 row and 4 columns, while w has 4 rows and 1 column.

Matrices as 2D Arrays

A matrix in MATLAB extends the idea of a vector to two dimensions. You can think of a matrix as a table of numbers. Each number is called an element and is identified by its row and column position.

To create a matrix, you use square brackets again. You separate elements in a row with spaces or commas, and you separate rows with semicolons. For example,

matlab
A = [1 2 3; 4 5 6];

creates a matrix with 2 rows and 3 columns. The first row is [1 2 3] and the second row is [4 5 6]. The size(A) command returns 2 3 which means 2 rows and 3 columns.

You can also write the same matrix over several lines to make it more readable:

matlab
A = [1 2 3 ...
     4 5 6];

In this case the line continuation ... tells MATLAB that the statement continues on the next line. The semicolon that separates rows can appear at the end of the previous line or at the beginning of the next, but for clarity beginners often write it at the end of each row inside the brackets.

The number of elements in a matrix is the product of its number of rows and columns. If A has $m$ rows and $n$ columns, then it has $m \times n$ elements. You can check the total number of elements with numel(A).

Although matrices are often associated with linear algebra, in MATLAB you will also use them simply as 2D data containers. For example, a grayscale image can be stored as a matrix, where each element represents a pixel intensity.

Dimensions and Shape of Arrays

The word dimension in MATLAB refers to the number of directions in which an array extends. A row vector and a column vector both have two formal dimensions, but one of them has size 1. For example, a row vector with 5 elements has size $1 \times 5$, and a column vector with 5 elements has size $5 \times 1$. A matrix with 3 rows and 4 columns has size $3 \times 4$.

You can find the shape of an array with size, and you can find out how many dimensions it has with ndims. For basic vectors and matrices, ndims usually returns 2. Higher dimensional arrays are possible, but they are introduced later.

When you create arrays, MATLAB also has a special notion of row major and column major storage. Internally MATLAB stores elements column by column, but as a beginner you rarely need to think about this detail. It becomes relevant when you explore performance and memory layout.

Size and shape are important because many operations in MATLAB check whether two arrays are compatible in their dimensions before they are combined. For instance, adding two matrices usually requires that they have the same size. The specific rules for operations on arrays are discussed in later chapters on arithmetic and elementwise operations, so here you only need to remember that the structure $m \times n$ is always present in the background.

Basic Creation Patterns for Vectors and Matrices

When you work with MATLAB, you rarely type all elements manually. Instead, MATLAB offers patterns to generate vectors and matrices in a compact and readable way.

The colon syntax is one of the most important patterns for vectors. It lets you create regularly spaced sequences. The form

$$
a:s:b
$$

represents the vector that starts at $a$, increases by step $s$, and does not go past $b$. For example

matlab
x = 1:5;        % gives [1 2 3 4 5]
y = 0:0.5:2;    % gives [0 0.5 1.0 1.5 2.0]

If you omit the step, MATLAB assumes a step of 1, so 1:5 is the same as 1:1:5. This syntax is particularly useful to create indices, time points, or sample positions for plots.

For matrices, a common pattern is to build them from existing row or column vectors. If you have two row vectors r1 and r2 of the same length, you can create a 2 row matrix by stacking them inside brackets with a semicolon:

matlab
r1 = [1 2 3];
r2 = [4 5 6];
B = [r1; r2];   % gives [1 2 3; 4 5 6]

Similarly, you can build a matrix by combining column vectors side by side, but the detailed syntax and indexing are handled in separate chapters. The key idea is that vectors act as building blocks for matrices.

Another useful pattern is to create arrays filled with zeros or ones of a given size. You use zeros(m, n) for an $m \times n$ matrix filled with 0, and ones(m, n) for one filled with 1. These functions are often used to initialize data structures of a certain size before you fill them with actual values. For example,

matlab
Z = zeros(3, 2);    % a 3-by-2 matrix of zeros
O = ones(1, 4);     % a 1-by-4 row vector of ones

You can also create simple identity matrices with a special function, but that belongs to the topic of special matrices.

Scalars as 1×1 Matrices

In MATLAB, a scalar can be viewed as a very small matrix with 1 row and 1 column. For example, if you set a = 5;, then size(a) returns 1 1. This viewpoint is consistent with how MATLAB treats computations. Many operations that accept a matrix also work with scalars, and combining scalars with arrays often uses implicit expansion rules.

Thinking of a scalar as a $1 \times 1$ matrix makes it easier to understand why some functions work the same way for both. For instance, numel(a) returns 1, and ndims(a) returns 2, just like any other 2D array with a single element.

Although this mathematical view is important, in everyday scripting you typically speak about scalars separately because they represent single values arithmetically.

Conceptual View of Linear Algebra in MATLAB

Vectors and matrices are not only containers for numbers, they also represent mathematical objects used in linear algebra. In many textbooks, a vector is written as a column, and a matrix multiplies a column vector on the left side. MATLAB follows these conventions closely.

When you see a column vector with $n$ elements, you can think of it as an element of $\mathbb{R}^n$. A matrix with size $m \times n$ can be thought of as a linear transformation that maps vectors from $\mathbb{R}^n$ to $\mathbb{R}^m$. The exact rules for matrix multiplication, transposes, and other linear algebra operations are explained in later chapters, but it is helpful to notice already that MATLAB syntax is designed to resemble the notation you see in mathematics.

Row vectors often represent data laid out along one dimension, for example time samples, and column vectors often represent variables or features. Matrices combine several such vectors either as rows or as columns. As you move forward, you will use matrices to solve systems of equations, perform transformations, and work with data sets.

Transpose and Orientation

Since row and column vectors are different orientations of the same set of numbers, MATLAB provides operations to switch between them. The transpose of a matrix or vector flips it around its main diagonal so that rows become columns and columns become rows.

If v is a row vector, then v.' is its nonconjugate transpose, which becomes a column vector with the same real values. If w is a column vector, then w.' is a row vector. For example,

matlab
v = [1 2 3];
w = v.';    % w is [1; 2; 3]

There is also a complex conjugate transpose, written as v' without the dot, which both transposes and takes the complex conjugate of each element. For purely real numbers, the two transpose operators produce the same numeric result. The difference becomes important when you work with complex numbers, which is covered in a later chapter.

Conceptually, the transpose is one of the simplest ways to control whether you are working with row or column vectors. It is frequently used when you want to ensure that your data has the correct orientation for a particular operation.

Viewing Vectors and Matrices in the Workspace

As you create vectors and matrices in MATLAB, they appear in the Workspace. You can interactively inspect their contents to build an intuitive feeling for their shape. If you click a variable in the Workspace browser, MATLAB opens it in a spreadsheet like Variable Editor, where each row and column is visible.

You can also display arrays directly in the Command Window by typing the variable name and pressing Enter. MATLAB prints the matrix with rows on separate lines and columns separated by spaces. For large matrices, MATLAB shows only a portion and provides information about the size. This helps you avoid flooding the Command Window and encourages you to use summary functions to understand your data instead of always printing everything.

Working with the Workspace and Variable Editor in detail is covered in a later section of the course, but it is worth mentioning here that these tools are especially helpful when you are learning how vectors and matrices behave.

Key points to remember:
Vectors and matrices are the core data structures in MATLAB.
Row vectors use spaces or commas between elements, column vectors use semicolons.
Matrices use semicolons to separate rows inside square brackets.
Size and shape, written as $m \times n$, describe how many rows and columns an array has.
Scalars behave like $1 \times 1$ matrices, so many matrix functions work on them too.
Transpose changes orientation between row and column, and prepares arrays for later linear algebra operations.

Views: 6

Comments

Please login to add a comment.

Don't have an account? Register now!