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:
= [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:
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):
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
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]
And, you could similarly use ".^", "+", and "-" with a vector and scalar. Here are some examples:
» c = a
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]
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:
= [2 : .25 : 3]
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]
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:
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:
= [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
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]
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):
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
You can change a number in a matrix by assigning to it:
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.