Basic Linear Algebra: Defining Vectors and Matrices

Vectors and Matrices what are they?

In Matlab everything is defined as either a vector or a matrix therefore it is imperative that you know what Vectors and Matrices are before you begin using Matlab.  The formal definition of a vector is something that has both a direction and a velocity. When using Matlab however, I think it is easier to think of a vector as either a row or a column of values. These values can be names, numbers, or anything you want. There are two kinds of vectors. These are row vectors and column vectors.

A Row Vector is a vector which has only One Row and can have as many columns as one wants. A row vector V with n columns is shown below. (invert the column one below, row vector image missing)

Alternatively, a column vector is a vector which has only ONE Column and can have as many rows as one wants. A column vector V with m rows is shown below.

column-vector

Matrices

A matrix is a rectangular array of quantities or expressions. I prefer to think of them as 2-D vectors. Matrices have both numerous rows and columns.

The typical representation of a matrix is m x n meaning the matrix has m rows and n columns.

matrix

The subscripts of each element refer to its position in the matrix. Matrices come in all different sizes and shapes. A matrix who has the same number of rows as columns is called a Square Matrix.

How to define Vectors and Matrices in MATLAB

Row Vectors

row-vector-description

The use of the semicolon at the end of each line ensures that Matlab will not print off the value of V in the Command Window. If you wish to have it display this value when running, remove the semicolon.

Column Vectors

When defining column vectors in Matlab use the semicolon (;) to separate rows.

column-vector-description

Accessing Different Vector Indices

To access different indices in a vector the following notation is used.

To access the third index or the value of 7 in both of the vector examples above one uses V(3). This tells Matlab that you are referencing the third value in the vector V.

Lets say you wish to multiply the third and fourth entry of the vector V and store that value in the first entry. You would type

V(1)=V(3)*V(4)

Matrices

Matrices in Matlab are defined by combining both row and column vectors. A 3 by 3 matrix in Matlab would be defined as follows.

matrix-matlab

MATLAB Examples

1) Define a 4 element row vector and then replace the third element by the product of the first and fourth.

vector-example1

2) Define a 3 element column vector and then put the value of 5 into the second element.

vector-example2

3) Define a 4by5 matrix and then make it so when you run the Matlab script, the element in the 3rd row and 2nd column will be printed.

matrix-example

Practice Problems

1) Define a 3 by 2 matrix and replace the value on the second row second column with the value on the third row first column.

2) Define a row vector with 6 elements.

3) Define a column vector with 2 elements and make sure they both print when you run Matlab (Hint: semicolon)