Lecture 2

Chapter 2 - Vectors and Matrices

Synopsis
In this class, we will review material from Chapter 1: Introduction to MATLAB and get an introduction to Chapter 2: Vectors and Matrices. The topics that will be covered include properties of vectors and matrices, array operations, matrix multiplication, and logical vectors.


Daily Quiz

Quiz 1


Chapter 1 Review

Lecture 1


Chapter 2

Properties of Vectors and Matrices

Vectors and matrices are MATLAB data structures that hold sets of values of the same type. This could be a list of 8-bit integers or a matrix of doubles. Vectors and matrices in MATLAB are the equivalent of 1- and 2-dimensional arrays in other languages.

The dimensions of a vector or matrix are commonly notated as r x c, where r is the number of rows and c is the number of columns. Here are some example of MATLAB vectors and matrices:

$$\begin{array}{cccc} \text{Scalar} & \text{Row Vector} & \text{Column Vector} & \text{Matrix} \\ [8]_{1x1} & \begin{bmatrix}1 & 6 & 4 & 2\end{bmatrix}_{1x4} & \begin{bmatrix}4 \\ 5 \\ 6\end{bmatrix}_{3x1} & \begin{bmatrix}1 & 4 & 6 & 2 \\ 6 & 2 & 3 & 1 \\ 8 & 9 & 1 & 7\end{bmatrix}_{3x4} \end{array}$$

Although it can be difficult to visualize, matrices can have any number of dimensions. The above matrix has two dimensions r and c, but we could just as easily create a matrix with three, four, or even ten dimensions. A common example of a three-dimensional matrix is an RGB image. If you imported an image from Instagram, it would have dimensions 512 x 512 x 3, where 3 is the color channels for red, green and blue.

Scalar and Array Operations

As we've highlighted before, MATLAB is great for operating on vectors and matrices. Imagine that you would like to multiply a set of numbers by some factor. This is called scalar multiplication, and we can perform this operation by using the * operator.

1
2
vec = [4 5 6];
vec * 2
ans =

     8    10    12

As you can see, the * operator allows us to multiply each value of the list by 2. We can perform similar elementwise operations on two vectors of the same dimensions. For example, we can add or subtract two vectors.

1
2
3
vec1 = [1 4 8];
vec2 = [6 2 9];
vec1 + vec2
ans =

     7     6    17

Elementwise operations on vectors and matrices are called array operations. What if we wanted to multiply the elements of two matrices? Here is where we have to be careful. Multiplication-based operations (multiplication, division, end exponentiation) have a very specific meaning in matrix algebra. If we want to perform elementwise multiplication on the two vectors above, we need to prepend the * operator with a dot (.*). We can prepend a dot to all multiplication-based operators to tell MATLAB that the operation should be performed element by element.

Matrix Multiplication

So, how do we perform true matrix multiplication? In order to multiply two matrices, they must be of the correct dimensions. More specifically, the number of columns in the first matrix must equal the number of rows in the second matrix.
$$ [A]_{mxn}[B]_{nxp}=[C]_{mxp} $$

The elements of matrix C are computed using the following equation:
$$ c_{ij}=\sum_{k=1}^{n} a_{ik}b_{kj}, $$

where \(c_{ij}\) is the element in C corresponding to the \(i^{th}\) row and the \(j^{th}\) column.

So long as they have the same number of elements, a row vector and a column vector can be multiplied in the same manner as matrices. Try multiplying a row vector by a column vector, and then try multiplying a column vector by a row vector. Do you get the same results?

In the world of vectors, there are two special types of multiplications--dot product and cross product. Dot product is simply a row vector multiplied by a column vector. When you tried this earlier, hopefully you noticed that the result is a scalar. Cross product is a bit more complicated, and is only defined for vectors with three elements. To compute the cross product, we organize the vectors as follows and perform standard matrix multiplication: $$ a\times b = \begin{bmatrix}0 & -a_3 & a_2 \\ a_3 & 0 & -a_1 \\ -a_2 & a_1 & 0\end{bmatrix} \begin{bmatrix}b_1 \\ b_2 \\ b_3\end{bmatrix} = \begin{bmatrix}c_1 & c_2 & c_3\end{bmatrix} $$

Logical Vectors

Although we mentioned that vectors and matrices must contain elements of the same type, we did not say what types they can be. Fortunately for us, we can use any variable type to compose our matrices. A particularly useful array is one composed of logical (0 or 1) values. These matrices are logically called logical vectors. These vectors can be the result of a relational operation on two vectors.

1
2
3
vec1 = [4 3 1 7];
vec2 = [5 3 2 7];
vec1 == vec2
ans =

     0     1     0     1

Logical vectors act similarly to logical variables. For example, we can use modified versions of the logical operators discussed in Lecture 1 to compare the elements of two logical vectors.

1
2
3
4
vec1 = logical([1 0 1 1 0 0]);
vec2 = logical([1 1 0 0 0 0]);
vec1 & vec2  %this is the elementwise logical and
vec1 | vec2  %this is the elementwise logical or
ans =

     1     0     0     0     0     0
ans =

     1     1     1     1     0     0

Final Words

Vectors and matrices are one of the most powerful features in MATLAB. They form the basis of MATLAB's vectorized programming ideology. Today we've gotten a brief introduction, and we will start to get our hands dirty in the next lecture.


Q&A

  • In Ouiz 1, why does myvar == 7 return ans = 1?
    myvar was initially assigned a value of 6. In line two, myvar was incremented by one. myvar == 7 is a relational expression that asks if the value in myvar is equal to the value 7. Since the value in myvar is 7, the relational expression is true. Because we are not assigning the result of the relational expression to a variable, MATLAB uses its default variable ans.