MATLAB Tutorial

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 completing the exercises in your training.

The Layout

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.

Getting Help and Quitting Matlab

Some helpful commands to know right away are the help and exit commands.

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).

The exit command can be used to exit a matlab session (you can also use the quit command for the same purpose).

Building a Matrix in Matlab

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 pi and 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 a, type:
clear a
(or type clear all to clear all variables).


Simple arithmetic functions can be performed as follows (of course, numbers or variables can be used):

Matrix arithmetic works in much the same way; addition and subtraction of matrices works just as you would expect. Try these just for practice:

There are three types of matrix multiplication in MATLAB:

The component wise function also works for division and exponents. Try:
A = 6*ones(3)
B = 3*ones(3)
C = A ./ B

Also, compare:
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.

Selecting Rows and Columns of a Matrix

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:

Other Tips

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 co-workers or supervisors would be happy to assist you.

Back to Your Training!