Introduction

 

In this lesson, you will be introduced to some basic math concepts, and their corresponding MATLAB data structures. You will see how these data structures can be created in MATLAB, and how they can be manipulated and used. Don't worry about understanding exactly what the data structures presented here are used for, or what exactly they mean mathematically, but just try to understand structurally what they are, and the mechanics of creating, modifying, and using them. You will learn more about what they mean mathematically, and what they are used for, in the core lessons later.

 

Scalars and Vectors

 

Three fundamental concepts in MATLAB, and in linear algebra, are scalars, vectors and matrices. A scalar is simply just a fancy word for a number. So, for example, "5" and "21.3", and pi are all scalars. In the tutorial lessons, we mostly just use the word number, but you will see the word "scalar" often if you consult the MATLAB help texts, so be sure to remember that it just means a number. A vector is an ordered list of numbers. As a very simple example, consider how you would represent a point in three dimensional space. There are three components to the point: the X, Y, and Z offsets from the origin. So, to represent the point you could store these three components in order in a vector of 3 numbers. Let's go ahead and see how we would store a point in MATLAB. Say the X, Y, and Z offsets of the point are 1,5, and 7. Then, in MATLAB you could create this point in one of two ways:

 

a_point1 = [1 5 7]

a_point1 =

1 5 7

a_point2 = [1; 5; 7]

a_point2 =

1
5
7

 

Notice the differences in the two ways the point can be created. The first way just has spaces between the numbers, while the second way has semicolons separating them. And, notice that the first way prints out the created vector as a row of the vector's numbers, while the second way prints out the created vector as a column of the vector's numbers. The first way created what is called a row vector, and the second way created a column vector. For now, don't worry about why there are two ways of creating a vector --- the reasons have to do with matrices, which we will cover later.

 

As stated above, the numbers stored in a vector are ordered; the ordering is such that the leftmost number in a row vector (topmost in a column vector) is first, and the rightmost number in a row vector (bottommost in a column vector) is last. You can access individual numbers in a vector by referring to them by their order in the vector. For example, here is how you print out the numbers in the vectors created above:

 

a_point1

a_point1 =

1 5 7

a_point2

a_point2 =

1
5
7

a_point1(1)

ans =

1

a_point1(2)

ans =

5

a_point1(3)

ans =

7

a_point2(1)

ans =

1

a_point2(2)

ans =

5

a_point2(3)

ans =

7

 

Elements and Element-By-Element Operations

 

An important terminological point concerning vectors, which might confuse you if not explained, is the use of the word element; you will see the word element in relation to vectors in the MATLAB help texts, and so it is important to understand what it means. Actually, it is really quite simple: an element of a vector is simply one of the numbers stored in the vector. So, for example, if you saw a sentence that said "a row vector of ten elements" or "a ten element row vector", then you would know that this just means a row vector that has ten numbers stored in it. Likewise, if you saw a sentence that said "the 8th element of the vector V", then you would know that this just means the number stored at the 8th position of V (i.e., V(8)). Finally, you will often see the expression "element-by-element" when reading MATLAB's help texts, and this has to do with operations that are performed on two vectors (both row vectors or both column vectors) of the same size to get another, result vector of the same size (and the same kind of vector, row or column, as the two vectors). This just means that, to get the value of a particular element in the result vector, you perform the operation on the corresponding (i.e., same position) elements in the two vectors. For example, "element-by-element multiplication" of two vectors [1 2 3] and [4 5 6] would give you [4 10 18]. The element-by-element operators in MATLAB are as follows:

 

element-by-element multiplication: ".*"

element-by-element addition: "+"

element-by-element subtraction: "-"

element-by-element exponentiation: ".^"

Here are some examples of using the element-by-element operators (notice that there is an error when trying to perform element-by-element operations when using a row vector and column vector together):

 

a

a =

1 2 3

b

b =

4 5 6

c

c =

4
5
6

d

d =

1
2
3

a .* b

ans =

4 10 18

a .* c
??? Error using ==> .*
Matrix dimensions must agree.

c .* d

ans =

4
10
18

a .^ b

ans =

1 32 729

c .^ d

ans =

4
25
216

 

You might wonder why the element-by-element multiplication and exponentiation operators have "." appended to the front of them, while the element-by-element addition and subtraction operators do not. The reason is that there are other kinds of multiplication and exponentiation operators for vectors, which are not element-by-element, that are denoted by "*" and "^".  For now, don't worry about what "^" does. "*", however, is a common and useful operation on two vectors of the same size (i.e., same number of elements) but different type (i.e., one of them a row vector, and the other a column vector), and the operation it performs is called the dot product. What the dot product does is that it first multiplies the corresponding elements (i.e., same position elements) of the two vectors, similar to what element-by-element multiplication does, and then adds up all the results of these multiplications to get a single, final number as the answer. A simple example should make this clear:

 

a = [1 2 3]

a =

1 2 3

b = [4 ; 5 ; 6]

b =

4
5
6

a * b

ans =

32

 

To get the answer "32", what MATLAB did was first to perform the multiplications of the corresponding elements of the two vectors: "1*4 = 4", "2*5=10", and "3*6=18". Then, to get the final answer of "32", MATLAB added all these multiplications together: "4+10+18=32". A final note about element-by-element operators is that you can use them with scalars (remember, that just means numbers!) and vectors together. For example, say you wanted to multiply every element of a vector by two; you could do that by performing element-by-element multiplication of the number 2 with a vector:

 

a = [1 2 3 4 5 6]

a =

1 2 3 4 5 6

b = a .* 2

b =

2 4 6 8 10 12

 

And, you could similarly use ".^", "+", and "-" with a vector and scalar. Here are some examples:

 

c = a .^ 2

c =

1 4 9 16 25 36

d = a + 2

d =

3 4 5 6 7 8

e = a - 2

e =

-1 0 1 2 3 4

 

Creating Vectors of Regularly Spaced Values and Using Vectors in Equations

One of the main, practical uses you will make of vectors is in plotting. We will talk about this more in the next lesson, but basically what you need to do before you can plot is to find the values of a function at a bunch of different points, and so you need a way to store a lot of points and the value of a function at those points conveniently. This is exactly what you can use vectors for. There is, in fact, a more convenient way than you learned above to create vectors that contain regularly spaced numbers. So, for example, say you want to store all of the even integers between 0 and 50 in a vector. Well, if the method of creating vectors presented above were the only way, then you would have to type quite a bit:

 

even_numbers = [2 4 6 8 10 12 14 16 ...

 

However, the even numbers follow a regular pattern and you can more easily create them as follows (note that the example below is creating a row vector --- the fact that MATLAB is saying "Columns 1 through 12", etc. doesn't mean that it is a column vector. This is MATLAB's way of printing out the elements of a vector when they can't all be printed out on one line. For now, just think of "Columns 1 through 12" as meaning "Elements 1 through 12", etc. Later, when we cover matrices, you will learn that a row vector can also be thought of as a matrix with only 1 row and many columns --- thus, what is really happening below is that MATLAB is telling you the values of the columns of a 1 row by 25 column matrix; likewise, a column vector can also be thought of as a matrix with only 1 column and many rows):

 

even_numbers = [2 : 2 : 50]

even_numbers =

Columns 1 through 12

2 4 6 8 10 12 14 16 18 20 22 24

Columns 13 through 24

26 28 30 32 34 36 38 40 42 44 46 48

Column 25

50

even_numbers(5)

ans =

10

 

In general, to create a row vector of regularly spaced values in MATLAB, you enclose three numbers inside brackets, separated by colons. The first number will be the first element in the vector, the middle number will be an increment (i.e., it represents the regular spacing of the values), and the last number will be an upper bound for the last element of the vector (i.e., the last element of the vector will be no greater than this). MATLAB will then create a row vector, starting from the first number that contains all numbers that are between the first number and last number, and that are at offsets from the first number in multiples of the increment. In other words, let S represent the first number, I represent the increment, and  L represent the last number. Then, if you typed "[S : I : L]" at the command prompt, MATLAB would create a vector that started with S. It would then add I to S to get the second element of the vector (i.e., the second element equals S + I). Then, it would get the third element by adding 2*I to S (i.e., the third element equals S + 2*I). Continuing, MATLAB would add 3*I to S to get the fourth element, 4*I to S to get the fifth element, and so on. However, MATLAB doesn't just go on adding numbers to the vector forever; when it gets to some element that is greater than L, it stops adding elements and leaves the vector as it is. So, for example, if S + 24*I is less than or equal to L, but S + 25*I is greater than L, then MATLAB would add S + 24*I as the last element of the vector. To make this concrete, let's see some more examples:

 

vector1 = [2 : .25 : 3]

vector1 =

2.0000 2.2500 2.5000 2.7500 3.0000

vector2 = [100 : 100 : 1000]

vector2 =

Columns 1 through 6

100 200 300 400 500 600

Columns 7 through 10

700 800 900 1000

vector3 = [1 : .3 : 3]

vector3 =

1.0000 1.3000 1.6000 1.9000 2.2000 2.5000 2.8000

 

You should make sure you understand the examples above, and also go ahead now and experiment by creating some vectors of your own in this way.

So, now you see how to create a vector of regularly spaced points. But, now you might wonder how you can create another vector containing the values of a function at the regularly spaced points. Well, this is where one of MATLAB's powerful features comes into play: you can use a vector as an input to an equation, similar to how you can use a variable as input, and get another vector as output which has elements that are the values of the equation evaluated at the numbers in the input vector. For example, say the equation you want to use is "y = x^2" (i.e., the equation for a parabola). So, just as you could plug in single numbers for x to get single output values for y (for example using x=5 gives you 25 as the value of y),  you can also plug in a vector for x and get a vector as output, where the output vector's numbers will be the results of the equation evaluated at the input vector's numbers (for example, using x = [1 2 3 4 5] as input to "y = x^2" gives you as output the vector [1 4 9 16 25] as y). Here are some examples of this:

 

input_points = [-10 : 1 : 10]

input_points =

Columns 1 through 12

-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1

Columns 13 through 21

2 3 4 5 6 7 8 9 10

parabola = input_points .^ 2

parabola =

Columns 1 through 12

100 81 64 49 36 25 16 9 4 1 0 1

Columns 13 through 21

4 9 16 25 36 49 64 81 100

input_points2 = [-pi : pi/8 : pi]

input_points2 =

Columns 1 through 7

-3.1416 -2.7489 -2.3562 -1.9635 -1.5708 -1.1781 -0.7854

Columns 8 through 14

-0.3927 0 0.3927 0.7854 1.1781 1.5708 1.9635

Columns 15 through 17

2.3562 2.7489 3.1416

sine_curve = 3*sin(5.*input_points2)

sine_curve =

Columns 1 through 7

0.0000 -2.7716 2.1213 1.1481 -3.0000 1.1481 2.1213

Columns 8 through 14

-2.7716 0 2.7716 -2.1213 -1.1481 3.0000 -1.1481

Columns 15 through 17

-2.1213 2.7716 0.0000

 

Matrices

 

As our final topic in this long lesson, we now turn to matrices. You aren't expected to understand the mathematical details of matrices (you will learn some of that in the core lessons and in the EE196 course). Here we will just tell you what a matrix is structurally, and how to create, modify, and access the elements of matrices. Basically, you can think of a matrix as a two-dimensional box of numbers, where the numbers are organized into rows and columns. For example, here is a matrix, in mathematical notation, with 4 rows and 3 columns:

 

matpic.gif (529 bytes)

 

In this example matrix, the first row contains the numbers 8, 12, and 9, the second row contains the numbers 7, 3, and 2, and so on. Likewise, the first column contains the numbers 8, 7, 12, and 8, the second column contains the numbers 12, 3, 4, and 1, and so on. Vectors, in fact, are really just special kinds of matrices. A row vector is simply a matrix with one row and a number of columns, and a column vector is simply a matrix with a number of rows and one column. A scalar is really even a very special kind of matrix: a matrix with exactly one row and one column! By the way, you will often see the word "array" when you consult the MATLAB help texts, so we should point out what this word means. Array is just a generic word for matrices and vectors; a vector is simply a one-dimensional array, while a matrix is a two-dimensional array --- so remember this when consulting the MATLAB help texts.

 

Creating a matrix in MATLAB is easy. There are actually two ways you can create matrices. To show how, let's create the above example matrix using the two methods. The first method is simply to list, all inside brackets, the numbers in rows separated by spaces, and to separate the rows with semicolons:

 

the_matrix = [8 12 9;7 3 2; 12 4 23; 8 1 1]

the_matrix =

8 12 9
7 3 2
12 4 23
8 1 1

 

The second method is to type the numbers in rows separated by spaces, and to hit return at the end of rows to start the next row on the next line, all of this inside of brackets:

 

the_matrix2 = [8 12 9
                           7 3 2
                         12 4 23
                           8 1 1]

the_matrix2 =

8 12 9
7 3 2
12 4 23
8 1 1

 

You can also create matrices from row vectors or column vectors, as long as all of the vectors being used to create the matrix have the same number of elements. You create a matrix from row vectors by listing the row vectors inside brackets separated by semicolons. And, you create a matrix from column vectors by listing the column vectors inside brackets separated by spaces. Here are some examples:

 

row_vector1 = [1 2 3]

row_vector1 =

1 2 3

row_vector2 = [3 2 1]

row_vector2 =

3 2 1

matrix_from_row_vectors = [row_vector1 ; row_vector2]

matrix_from_row_vectors =

1 2 3
3 2 1

column_vector1 = [1;3]

column_vector1 =

1
3

column_vector2 = [2;8]

column_vector2 =

2
8

matrix_from_column_vectors = [column_vector1 column_vector2]

matrix_from_column_vectors =

1 2
3 8

 

Now let's talk briefly about some important operations on matrices. You can access an element in a matrix by referring to the row and column it is in inside the matrix. For example (note that the row number is first, followed by the column number):

 

matrix_from_row_vectors(2,2)

ans =

2

matrix_from_row_vectors(2,3)

ans =

1

 

You can also extract any contiguous subset of a matrix, by referring to the row range and column range you want. For example, if "mat" is a matrix with 5 rows and 8 columns, then typing "mat(2:4,4:7)" would extract all elements in rows 2 to 4 and in columns 4 to 7. Here is an example:

 

mat = [1 8 9 4 5 2 4 8
              9 3 7 4 8 2 8 8
              1 2 9 4 6 2 3
            10 2 2 4 6 7 3 2
              3 12 3 3 2 8 4 9]

mat =

1 8 9 4 5 2 4 8
9 3 7 4 8 2 8 8
1 2 9 4 6 2 3 4
10 2 2 4 6 7 3 2
3 12 3 3 2 8 4 9

new_mat = mat(2:4,4:7)

new_mat =

4 8 2 8
4 6 2 3
4 6 7 3

 

You can change a number in a matrix by assigning to it:

 

new_mat

new_mat =

4 8 2 8
4 6 2 3
4 6 7 3

new_mat(2,3) = 1999

new_mat =

4 8 2 8
4 6 1999 3
4 6 7 3

 

You should also keep in mind that, since vectors are just kinds of matrices, all the operations you learned above for matrices can also be used for vectors. For example, you can change numbers stored in vectors as above. There are many other aspects of matrices and vectors, and operations on them that we have not gone into here in this introductory lesson. We will go into some more advanced aspects of matrices and vectors in the core lessons, but you should also explore the MATLAB help texts on your own to learn more about how you can use and manipulate matrices in MATLAB.

 

To continue on to the next lesson, click here.