Matlab is a tool for modeling, prototyping, and investigating computational and mathematical systems. It is an interpreted computer language with lots of support for user interfaces, graphics, and mathematics built-in. What's more, it's extensible and (almost completely) platform-independent, so that you can easily incorprorate others' systems and experiment with them. If you already know matlab, skip to the next section. There are many excellent on-line references, e.g., this one.
To start matlab in the CS labs, click on the desktop and select the "Go" menu from the top of the screen. Choose "Applications" from that menu and then "Additional Applications" from there. You will see a "Math" folder, in which is "Matlab." There are classroom and research licenses. Even if you're planning to get phd in spam-finding, choose the "Classroom" and then double-click on the Matlab icon (an orange-ish surface plot).
Just to be sure you know matlab is working, see if it can answer important questions like
>> 40+2If it can handle this, handling images should be no problem. You might want to try
>> 40+2;The semicolon at the end of a statement is not a terminator, but suppresses matlab's printing of the evaluation of the expression.
Variables work as you may expect them to in matlab.
>> x = 42;followed by
>> x * 0will convince you that the system has indeed remembered
x's value.
The variable ans always holds the result of the last computation.
Matlab variables are case-sensitive.
If you want to clear the value of a variable you can use
>> clear xThe command
>> clearclears all variables. If you want to check up on your variables, try the commands
>> who >> whos
A big reason for using matlab is that it has considerable support for vector and matrix manipulation. For example, you can define a vector and a matrix as follows:
>> x = [ 1 2 3 ]This declares
x to be a 1 by 3 row vector.
>> A = [ 1 0 1 ; x ]This declares
A to be a 2 by 3 matrix. Note that
the semicolon here indicates that you want to start a new row.
Also, matlab lets you embed matrices within one another, as long
as their dimensions work out.
The transpose of x is
>> x'and multiplication is as you might guess
>> b = A * x'If your dimensions don't coincide, matlab will warn you. It will let you do something like
>> x + 5(componentwise), however.
>> helpTo get more detailed information about one of the topics, try
>> help generalwhich provides yet more topics. Finally
>> help pathgives you a matlab "manual page" about the
path
command (where matlab will look for files). You may also want to set your
MATLABPATH shell variable to add your direcctories
to your search path. Alternatively, you can change your "current
directory" using the interface on the left. On knuth (when on a CS machine),
My desktop was at
/private/var/automount/mnt/home/dodds/Desktop. (But don't
use mine!)
It's usually easiest to store commands in files and run them from there. There are two basic types of matlab "m" files: scripts and functions. As an example script, type the following into a file. To do this, choose "File" -- "New m-file" and then type
x = 5; y = 10; answer = y*x - y + y/xSave this file as
test.m, perhaps on the desktop or
a folder for this assignment. YouThen if you type
>> testat the matlab prompt, you will find the answer you sought. If matlab complains about not being able to find the file, then you put it in a directory not in its search path (the current directory is in the search path by default). Either move it to matlab's current directory, change matlab's current directory, or set the
path variable. You can get help on
path, cd, or pwd.
The second kind of m file is a function definition. Here's an example: type
function z = fun(x,y) % z = fun(x,y) computes x to the y power a = x^y; z = a;into a file named
fun.m and then try
>> fun(3,5)The
% is matlab's one-line comment character.
The comment in the above file is actually used by the help
system. Type
>> help funto remind yourself what it does in case you forget... .
Note: there was no formal code to write or problems to solve in this section of the assignment.