The MATLAB Matrix - MATLAB's Namesake

The MATLAB matrix is the big brother of the MATLAB vector. While a vector consists of a single row or column of values, a matrix is an entire two-dimensional array of values. In fact, matrices in MATLAB are one of the most important ways to store and manipulate data (especially if you remember that MATLAB stands for MATrix LABoratory).

In this post, I will introduce the MATLAB matrix, including how to create matrices and how to manipulate them. I will also introduce some of the useful built-in functions we can use with matrices.

Constructing a 2D Matrix in MATLAB

Just as with vectors, we specify matrices by enclosing their contents with square brackets [ ]. The individual values in a MATLAB matrix are referred to as elements.

A MATLAB matrix is effectively built row-by-row. The contents of each column are separated and either spaces or commas (,)and rows are separated by semicolons (;). Note this is exactly the same syntax we used for row and column vectors!

For example, we can specify the rows and columns of of a matrix in one statement as:

>> A = [5 35 43; 4 76 81; 21 32 40]

A =

     5    35    43
     4    76    81
    21    32    40

If I am working in a script file, I could also specify each row of a MATLAB matrix on a new line as shown below. If you do this, don't forget to enclose all of the contents in square brackets!

A = [5  35 43
     4  76 81
     21 32 40]

MATLAB Matrix Indices

Each element in a matrix can be located using what we call its row and column index. For example, in the matrix A from above, the element containing the number 4 has the index A(2,1). This tells us that the element is located in the 2nd row, 1st column. We can fetch this information using the index as:

>> A(2,1)

ans =

     4

We can also specify vectors of indices to obtain subsets of a matrix or use a colon (:) as an index to fetch entire rows or columns. For example, the index below obtains rows 1 and 2, all columns of the matrix A.

>> B = A(1:2,:)

B =

     5    35    43
     4    76    81

In another example, we can use indices to get the elements at the intersection of the rows 1 and 3, columns 1 and 3.

>> C = A([1,3],[1,3])

C =

     5    43
    21    40

Updating Matrix Entries

Matrix indices can also be used to update the elements within a matrix. We can do this by simply specifying the new value to put inside the given indices. The input below replaces the 2nd row, 3rd column of A from above with the value 987.

>> A(2,3) = 987

A =

     5    35    43
     4    76   987
    21    32    40

Finally, we can use vector indices (just like earlier!) to replace larger sections of a matrix. Below I replace the values at the intersection of rows 1 and 3, columns 1 and 3 with the value 99.

>> A([1,3],[1,3]) = [99 99; 99 99]

A =

    99    35    99
     4    76   987
    99    32    99

Now that we know how to build and manipulate matrices in MATLAB, in the next post we will discuss some basic MATLAB matrix operations and simple functions that use matrices: Useful Matrix Operations in MATLAB.

icon-long-arrow-left Last icon-cog Next icon-long-arrow-right
MATLAB Matrix Operations

Leave a Reply

Your email address will not be published.