Lesson 2.6

Matrices and Vectors

Summary

  • Understand the basics of vectors and matrices.
  • Program vectors and matrices in MATLAB.
  • Manipulate vectors and matrices with common MATLAB syntax.
  • Revisit strings by viewing and referencing them as vectors.

Note: if you already understand what a vector and a matrix are, skip down to this lesson’s section on programming them in MATLAB.

What Is a Matrix?

A matrix is a special organized way of representing groups of numbers. This organized form is similar to a grid or table, but where each cell, or “element”, has a standardized reference. Matrices have two dimensions/directions called rows and columns. (Two dimensional matrices can be stacked to form three dimensional matrices, but we do not need to worry about this for now). In Figure 1, we see the standard notation for referencing an element in a matrix.


Figure 1: Generalized example of a matrix (size: mxn).

Figure 2 shows an example of how a matrix of numbers is often written on paper. In the bottom right of the figure we have noted the size of the matrix (the number of rows and columns). However, this is not mandatory or standard to write with each matrix explicitly.

Important Note: When reporting the size of a matrix, the rows always come first followed by the columns (e.g., rows by columns or 2x3 in Figure 2).


Figure 2: A matrix with 2 rows and 3 columns (size: 2x3).

Special Types of Matrices

Some matrices are arranged in a way that is useful to define as a special type of matrix for linear algebra. Some examples of this are an identity matrix, a symmetric matrix, and an upper/lower-triangular matrix. MATLAB even has dedicated functions to create that some of these matrices. For example, eye() creates an identity matrix of any size you input. We will go more into these special matrices in later lessons.

What Is a Vector?

A vector is a category of matrices, a one dimensional matrix. This means it has only one row (a row vector - see Figure 3) or one column (a column vector - see Figure 4). As before, the numbers to the bottom right of the vector denote the size of the vector.

Important Note: Since a vector is just a type of matrix, the rules, notation, and syntax for matrices in MATLAB also apply to vectors.


Figure 3: Generalized example of a column vector (size: nx1).

Figure 4: Generalized example of a row vector (size: 1xn).

You can see an example of a vector containing numbers in Figure 5. Do not confuse programming (MATLAB) vectors with a general euclidean vector, which has a magnitude and direction (e.g., 3 meters at 45 degrees). Also note that an array can often be a vector or a matrix; however, in MATLAB the term array more often refers to a vector.


Figure 5: A vector with 1 row and 3 columns (size: 1x3).

Creating a Matrix in MATLAB

Creating a matrix in MATLAB is fairly simple: a space (" ") creates a new column, and a semicolon (;) creates a new row. Example 1 shows how vectors and matrices can be implemented in MATLAB and how MATLAB formats them when output to the Command Window.

Example 1: Entering Simple Matrices and Vectors
Editor
rowVector = [1 2 3 4] %Creating a row vector columnVector = [1; 2; 3; 4] %Creating a column vector %NOTE: a space (" ") creates a new column, and a semicolon (;) creates a new row matrixA = [1 2 3; 4 5 6] %Creating a 2x3 matrix matrixB = [5 6; %You can move new rows down a line for clarity if you like. As discussed 7 8] % previously, MATLAB does not care about whitespace. Command Window Output
rowVector = 1 2 3 4 columnVector = 1 2 3 4 matrixA = 1 2 3 4 5 6 matrixB = 5 6 7 8 [Try this code yourself with Octave Online! Click Here]

A matrix can be the result of joining vectors, or concatenating vectors, which is especially useful in programming. That is, creating a matrix with a set of side-by-side vectors. You can see a trivial example of this in Example 2. Note that the size() command can be used to output the matrix or vector total size. In Lesson XXXX, we will cover Systems of Equations (LINK TO Linear Algebra) where this concept has an obvious real world application.

Example 2: Concatenating Vectors
Editor
%Defining row vectors vector1 = [1 1 1] %Leaving output unsuppressed as an example vector2 = [2 2 2]; vector3 = [3 3 3]; fprintf('You can verify these are row vectors from their size: %.0f x %.0f.\n',size(vector1)) %Now, we can combine these vectors in a new vector, which will result in a matrix if we stack them %"on top" of each other. matrix = [vector1; vector2; vector3] %Entering the vectors as rows in the new "vector" %If we stack the row vectors side by side, we will get one long row vector. longVector = [vector1 vector2 vector3] %Entering vector as columns in the new vector Command Window Output
vector1 = 1 1 1 You can verify these are row vectors from their size: 1 x 3. matrix = 1 1 1 2 2 2 3 3 3 longVector = 1 1 1 2 2 2 3 3 3 [Try this code yourself with Octave Online! Click Here]

Referencing a Matrix in MATLAB

Referencing, or indexing, matrices in MATLAB is done in the same way you do it on paper as we discussed above. Each element, A(i,j), is called with row number first and column second. Simple! We will go more into depth with how to reference different locations of a matrix in the lesson on Working with Matrices and Loops (LINK TO LESSON).

Important Note: MATLAB indexes start from 1 not 0 as in some other programming languages. That is, the index of the first element of a vector is 1.

Example 3: Referencing/Indexing a Vector/Matrix
Editor
matrix = [3 2 1;6 5 4] %Defining a matrix with two rows and three columns vector = [1.0 9 8 7] %Defining a row vector row = 2; column = 3; matrixElement = matrix(row,column); %Referencing/Indexing one matrix element fprintf('The element from row %.0f, column %.0f is %g.\n',row,column,matrixElement) vectorElement1 = vector(3) %Referencing/Indexing one vector element vectorElement2 = vector(1,3) %Referencing/Indexing the same vector element %Note: Since there is only one dimension in a vector, we do not need to include % the redundant "1" (ie. vector(1,3). However, including it will also work, of course. Command Window Output
matrix = 3 2 1 6 5 4 vector = 1 9 8 7 The element from row 2, column 3 is 4. vectorElement1 = 8 vectorElement2 = 8 [Try this code yourself with Octave Online! Click Here]

Basic Functions and Commands for Matrix Manipulation

Some common and useful functions when working with matrices (or vectors) in MATLAB are given below. We cover:

  • end: references/indexes the last element in that dimension
  • length(): outputs the maximum dimension
  • size(): supplies the total number of rows and columns: the size of the matrix
  • A(:,1): yields the entire first column. The “:” references “all of that dimension”.
  • A(1:20,1): yields rows 1 through 20 in column 1.

Example 4: Functions/Commands for Matrices
Editor
matrix = magic(4) %Creating a special 4x4 (square) matrix matrixDimensions = size(matrix) %This returns a vector of the form [rows, columns] fprintf('The matrix A has %.0f rows and %.0f columns.\n',matrixDimensions(1),matrixDimensions(2)) vector = matrix(:,1) %Creating a vector from the first column of the defined matrix vectorLength = length(vector) %Finding the length of the vector %Note: The function length can be useful for vectors since we do not need. % to know whether it is a row or a column vector. elements1 = matrix(1:3,1) %Creating a vector from the FIRST 3 elements of A elements2 = matrix(end-2:end,1) %Creating a vector from the LAST 3 elements of A Command Window Output
matrix = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 matrixDimensions = 4 4 The matrix A has 4 rows and 4 columns. vector = 16 5 9 4 vectorLength = 4 elements1 = 16 5 9 elements2 = 5 9 4 [Try this code yourself with Octave Online! Click Here]

Strings as Vectors

Earlier in this chapter, we discussed Characters and Strings (LINK TO LESSON). In MATLAB and many other programming languages, strings, or arrays of characters, can be referenced the same way as a vector, which we can see in Example 4. This can be especially useful if we only want to look at a particular part of a string/text.

Example 5: Strings as Vectors
Editor
someString = 'This is an example string.'; %Defining an arbitrary string %Referencing specific parts of the string subString = someString(1:10) %The first 10 elements/characters of the string lastCharacter = someString(end) %The last element of the string stringLength = length(someString) %Finding the length of the string Command Window Output
subString = This is an lastCharacter = . stringLength = 26 [Try this code yourself with Octave Online! Click Here]

Next Lesson

In the next lesson, we will begin a new topic on plotting so we can start visualizing data!