Syntax
How Matlab Works
Matlab works by executing the mathematical statements you enter in the command window. By default, any output is immediately printed to the window.You are also allowed to assign a name to an expression for your convenience. Keep in mind that the name you assign is only a name, and it does not represent a mathematical variable (as it would in Maple, for example). Every name must have a value at all times. If you try to read the value of an unassigned name, you will get an error.
Nearly everything in Matlab in a matrix, whether it looks like it or not. This takes some getting used to. We'll be introducing matrix-style operations along with their scalar counterparts so you can understand the patterns that arise in the syntax.
Symbols and Punctuation
Matlab was designed to use fairly standard notation. Try these examples on your own computer.| Input | Output | Comments |
|---|---|---|
| 2 + 3 7-5 34*212 1234/5786 2^5 |
ans = 5 ans = 2 ans = 7208 ans = 0.2173 ans = 32 |
Arithmetic works as expected. Note that the result is given the name "ans" each time. |
| a = sqrt(2) | a = 1.4142 | You can choose your own names for things. |
| b = a, pi, 2 + 3i | b = 1.4142 ans = 3.1416 ans = 2.0000 + 3.0000i |
You can use commas to put more than one command on a line. Pi, i, and j are contants. |
| c = sin(pi) eps |
c = 1.2246e-016 ans = 2.2204e-016 |
"eps" is the current limit of precision. Anything smaller than eps is probably zero. Note that Matlab understands (and expects you to understand!) scientific notation. |
| d = [1 2 3 4 5 6 7 8 9] e = [1:9] f = 1:9 |
d = 1 2 3 4 5 6 7 8 9 e = 1 2 3 4 5 6 7 8 9 f = 1 2 3 4 5 6 7 8 9 |
"d", "e", and "f" are all vectors. They are equal. Note the use of the ":" operator - it counts (by ones) from one number to the next. |
| g = 0:2:10 f(3) f(2:7) f(:) |
g = 0 2 4 6 8 10 ans = 3 ans = 2 3 4 5 6 7 1 2 3 4 5 6 7 8 9 |
More uses of the colon. Note that you can use it to get slices of a vector (or matrix, or cube, etc), or get the whole thing. |
| h = [1 2 3]; h' |
(nothing) ans = 1 2 3 |
A semi-colon ";" will prevent the output from being displayed. A single quote " ' " computes the transpose of a matrix, or in this case, switches between row and column vectors. |
| h * h' h .* h h + h |
ans = 14 ans = 1 4 9 ans = 2 6 8 |
Operations on vectors. * is matrix multiplication, and so the dimensions must line up correctly. " .* " is entry-by-entry multiplication. |
| g = [ 1 2 3; 4 5 6; 7 8 9] | g = 1 2 3 4 5 6 7 8 9 |
Entering a matrix. |
| g(2,3) g(3,:) g(2,3) = 4 |
ans = 6 ans = 7 8 9 g = 1 2 3 4 5 4 7 8 9 |
Accessing matrix elements. Note use of ":" to access an entire row. |
| g^2 g .^ 2 |
ans = 30 36 42 66 81 96 102 126 150 ans = 1 4 9 16 25 36 49 64 81 |
The first multiplies the matrix by itself. The second squares each entry in the matrix. |
How to Control Output
Before we go any deeper into matrices, it would be wise to mention formatting issues.Two useful commands are format and more.
- To control linespacing, use format compact.
- To see all 15 digits that were used in calculation, use format long .
- To see just 5 digits, use format short .
- To supress output completely, use a semi-colon at the end of the command.
- To see one page of output at a time use more on.
To see other options, type help format or help more .
Note that Matlab always uses "double" precision (about 15 digits) in its calculations. These commands merely adjust the display.



