MATLAB (for "Matrix Laboratory") is a language and interactive environment for numeric computation, algorithm creation, etc. This program is incredibly powerful and versatile and can perform a wide variety of operations, but for now we will only focus on the basics necessary for this module.
If you do not have access to a computer with Matlab, we will try to give you an idea of the results of the Matlab experiments. This way, you can still answer some of the questions following these activities.
Once you have opened MATLAB, you will see something like this:
(Of course, you may see something slightly different, depending on which version of MATLAB you are using)
The Command Window is where we will be doing most of our work.
Some helpful commands to know right away are the
help command (as you may have guessed) brings up an online help menu. You can also ask for help on a specific function by typing
help fn (where
fn is any matlab function).
exit command can be used to exit a matlab session (you can also use the
quit command for the same purpose).
Since MATLAB is a matrix laboratory, it makes sense that there are many types of matrices built into the program.
This should yield a 5x5 random matrix with entries between 0 and 1.
Other useful built-in matrices include:
Look up each of these with the
help command and practice creating a few of them in the command window.
You can also build a matrix from scratch, if you like. Try it! Type:
[1, 2, 3; 4, 5, 6; 7, 8, 9]
(row entries can be separated by spaces or commas, whichever you prefer, and columns are separated by semi-colons).
It's useful, a lot of times, to store a value in a variable. For example:
a = 5
tells MATLAB to assign a value of 5 to
a (by convention, matrices are generally stored in upper-case variables
A, B etc.).
MATLAB also has many built-in variables, such as
eps. To get the value of each of these, simply type them into the command window and hit return, or use
help pi, etc.
One important built-in variable is
ans, which stores the last output value not assigned to a variable.
To check which variables have values assigned to them, use:
To clear a variable
clear all to clear all variables).
Simple arithmetic functions can be performed as follows (of course, numbers or variables can be used):
4 + 5
4 - 5
4 * 5
4 / 5
Matrix arithmetic works in much the same way; addition and subtraction of matrices works just as you would expect. Try these just for practice:
A = [1 2 3; 4 5 6] + [1 2 3; 4 5 6]
B = A - [1 2 3; 4 5 6]
There are three types of matrix multiplication in MATLAB:
C = 3 * ones(4)
D = A * B'
E = A.*B
The component wise function also works for division and exponents. Try:
A = 6*ones(3)
B = 3*ones(3)
C = A ./ B
C ^ 2
C .^ 2
MATLAB also recognizes component wise addition and subtraction, but, of course, it is superfluous to use them since addition and subtraction work component wise anyway.
Create the following matrix:
A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
Then select all rows of the second column with the command:
Select all rows of the third column with the command:
You can also select multiple rows or columns at once:
Recall your entries in the command window with the up arrow.
Suppress unnecessary output with a semi-colon:
A = ones(4)
A = ones(4);
are creating the same matrix A, you just don't get needless output with the second command.
This was a very quick and basic introduction. If, in the course of your work, you run across any problems that I haven't covered, try using the
help function I told you about. If that doesn't answer your question, I'm sure one of your fellow students or professors would be happy to assist you.
This material is based upon work supported by the National Science Foundation under Grant No. 0546622. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.