Student Center
Performing Calculations
- Prerequisites and Expectations for the Tutorial
- A Very Elementary MATLAB Tutorial
- Starting MATLAB
- Running the Demos
- Getting Help
- Creating Variables
- Performing Calculations
- Visualizing Data
- Creating Scripts with MATLAB Editor/Debugger
- Saving Variables and Sessions
- Working with Files, Directories and Paths
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 1) created in MATLAB, and how they can be 2) index and 3) used.
MATLAB, the MATrix LABoratory
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 (a single value).
A vector is an ordered list of numbers (one-dimensional). In MATLAB they can be represented as a row-vector or a column-vector.
A matrix is a rectangular array of numbers (multi-dimensional). In MATLAB, a two-dimensional matrix is defined by its number of rows and columns.
Note: Though matrices can have more than two-dimensions, in this lesson we work only with two-dimensional matrices. If you need to work with matrices that have more than two-dimensions, you can refer to the help information of MATLAB.
In MATLAB, and in linear algebra, numeric objects can be categorized simply as matrix: Both scalars and vectors can be considered a special type of matrix. For example a scalar is a matrix with a row and column dimension of one (1-by-1 matrix). And a vector is a one-dimensional matrix: one row and n-number of columns, or n-number of rows and one column.
All calculations in MATLAB are done with "matrices". Hence the name MATrix LABoratory.
Creating Matrices in MATLAB
In MATLAB matricies are defined inside a pair of square braces ([]). Punctuation marks of a comma (,), and semicolon (;) are used as a row separator and column separator, respectfully.
Note: you can also use a space as a row separator, and a carriage return (the enter key) as a column separator as well.
Below are examples of how a scalar, and a vector can be created in MATLAB.
my_scalar = 3.1415
my_scalar = 3.1415
my_vector1 = [1, 5, 7]
my_vector1 =
| 1 | 5 | 7 |
my_vector2 = [1; 5; 7]
my_vector2 = 15
7
You will note that one vector is represented as a row vector (my_vector1), and the other as a column vector (my_vector2).
Now you know how to create scalars and vectors, but what about a two dimensional matrix? For example, how do we create a matrix called my_matrix with the numbers 8, 12, and 19 in the first row, 7, 3, 2 in the second row, 12, 4, 23 in the third row, and 8, 1, 1, in the fourth row?
my_matrix = [8, 12, 19; 7, 3, 2; 12, 4, 23; 8, 1, 1]
my_matrix =
| 8 | 12 | 19 |
| 7 | 3 | 2 |
| 12 | 4 | 23 |
| 8 | 1 | 1 |
With the above line of code, my_matrix is now defined as the 4-by-3 matrix (4 rows and 3 columns).
You can also combine different vectors and matrices together to define a new matrix. But remember that the output needs to be a valid rectangular matrix. Note that the row separator and column separator function in the same way.
row_vector1 = [1 2 3]
row_vector1 =
| 1 | 2 | 3 |
row_vector2 = [3 2 1]
row_vector2 =
| 3 | 2 | 1 |
matrix_from_row_vec = [row_vector1 ; row_vector2]
matrix_from_row_vec =
| 1 | 2 | 3 |
| 3 | 2 | 1 |
column_vector1 = [1;3]
column_vector1 =
| 1 |
| 3 |
column_vector2 = [2;8]
column_vector1 =
| 2 |
| 8 |
matrix_from_col_vec = [column_vector1 column_vector2]
matrix_from_col_vec =
| 1 | 2 |
| 3 | 8 |
my_matrix = [8, 12, 19; 7, 3, 2; 12, 4, 23; 8, 1, 1]
my_matrix =
| 8 | 12 | 19 |
| 7 | 3 | 2 |
| 12 | 4 | 23 |
| 8 | 1 | 1 |
combined_matrix = [my_matrix, my_matrix]
combined_matrix =
| 8 | 12 | 19 | 8 | 12 | 19 |
| 7 | 3 | 2 | 7 | 3 | 2 |
| 12 | 4 | 23 | 12 | 4 | 23 |
| 8 | 1 | 1 | 8 | 1 | 1 |
From these examples you can see how any type of matrix can be created using the square braces, in conjunction with the row and column separators. And with this you can create the data set needed to compute with or visualize with.
More than not, the type of data that you will work with will be vectors. For example you may be given the temperature data for each hour of the day (a vector with 24 elements), and need to plot the data against time. But how would you create the time data? You could create it manually,
time = [0, 100, 200, 300, 400, ..., 1900, 2000, 2100, 2200, 2300];
or you could use the colon operator. The colon operator allows you to create an incremental vector of regularly spaced points by specifying:
start_value:increment:stop_value
In this case,
time = 0:100:2300
Instead of an incremental value you can also specify a decrement as well. The following line of code would create a time vector from 2300 to zero with decrements of a 100.
time_dec = 2300:-100:0Notes:
- In the next lesson you will learn how you can actually plot the data.
- If you want to load in your data from an external source perform a search on "import data" in the search tab of the MATLAB Help Browser.
Indexing Into a Matrix
Once a vector or a matrix is created you might needed to extract only a subset of the data, and this is done through indexing.
To index into an ordered list, a vector, you need to know where the order starts. In a row vector the left most element has the index of one. In a column vector the top most element has the index of one.
Let’s look at the example of my_vector1 and my_vector2 and see how we can index into its values.
my_vector1 = [1 5 7]
my_vector1 =
| 1 | 5 | 7 |
my_vector2 = [1; 5; 7]
my_vector2 =
| 1 |
| 5 |
| 7 |
my_vector1(1)
ans = 1
my_vector2(2)
ans = 5
my_vector1(3)
ans = 7
my_vector2(1)
ans = 1
my_vector2(2)
ans = 5
my_vector2(3)
ans = 7The process is much the same for a two-dimensional matrix. The only difference is that you would specify the row and column index rather than the single index, as we did for the vectors.
my_matrix = [8, 12, 19; 7, 3, 2; 12, 4, 23; 8, 1, 1]
my_matrix =
| 8 | 12 | 19 |
| 7 | 3 | 2 |
| 12 | 4 | 23 |
| 8 | 1 | 1 |
To access the value of 4 you would type in
my_matrix(3,2)
ans = 4
Note: 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:


new_mat = mat(2:4,4:7)
You can change a number in a matrix by assigning to it:
new_mat
new_mat(2,3) = 1999
You should 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 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.
Element-By-Element Operations and Matrix Operations
Before anything else, let us define what an element is: an element of a matrix is simply one of the numbers stored in the matrix.
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)).
When reading MATLAB documentation you will often see the expression "element-by-element", and this has to do with operations that are performed on two matrices of the same size to get another, result vector of the same size. 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 division: "./"
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 = 45
6
d
d = 12
3
a .* b
ans = 4 10 18
a .* c
??? Error using ==> .*
Matrix dimensions must agree.
c .* d
ans = 410
18
a .^ b
ans = 1 32 729
c .^ d
ans = 425
216
An additional 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 12And, 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 4You 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, division, and exponentiation operators for matrices, which are not element-by-element, that are denoted by "*" , "/"and "^".
This brings us to the other type of operations: matrix operations. Element-by-element operations allow us to compute things on an element-by-element basis, but matrix operations allow us to perform matrix-based computation.
For example, the multiplication of two matrices, represented by "*", performs a dot product of the two matrices. 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 = 45
6
a * b
ans = 32To 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".
Putting It All Together
Having gone through this section, you now know how to create your own matrices (using the square braces or the colon operator), index into the matrices, and compute with the matrices (element-by-element computations, and matrix-based computations).
Let’s now look at an example that uses these concepts. Let’s say that you are given the task of finding the solutions to the equation of a parabola (y=x^2). Where "x" is the range from -100 to 100. Once computed, we want to only look at the data from the range -2 to 2.
With what we know we could do the following:
inc = 1; % you can specify your own value of INC
x = -100:inc:100;
y =
x.^2; % compute the square of each element separately
Note: The above is an example of one of MATLAB's powerful features, vectorization: using 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.
Now, we need to locate the data points from -2 to 2. To do so, we need to know the length of "x", and index into both "x" and "y" at the right locations.
We can either look for a function which will return the length of a vector, or figure it out ourselves. Can you find the function for obtaining a vector’s length? Or are you figuring it out in your head?
len_x = length(x)
len_x = 201
midpoint_index = round(len_x/2)
midpoint_index = 101
new_x_range = x(midpoint_index-2:midpoint_index+2)
new_x_range = -2 -1 0 1 2
new_y_range = y(midpoint_index-2:midpoint_index+2)
new_y_range = 4 1 0 1 4Note: This is not the only way to do this example. You could have also found the range using Boolean indexing. Try searching the Help Browser on "Boolean".
Below is another example of vectorization.
input_points = [-pi : pi/8 : pi]
input_points =
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_points)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
Continue on to the next lesson.
Store