| General Purpose Commands | ||
|---|---|---|
| Command | Description | Example |
| help | On-line help for Matlab commands | help plot |
| ^C | Terminate execution of current command | ^C (control-C) |
| quit | Quit Matlab session | quit |
| cd | Change current working directory | cd D:\mymatlab |
| who | List current workspace variables | who |
| clear | Clear variables and functions from memory | clear |
| edit | Edit m-file (script file or function) | edit myscript.m |
| Predefined Variables | |
|---|---|
| Variable | Description |
| ans | Default variable used for results |
| i and j | Stands for square root of -1 |
| Inf | Stands for infinity |
| NaN | Stands for Not a Number |
| pi | 3.14159265358979 |
| Special Characters | |
|---|---|
| Symbol | Description |
| + | Addition (matrix and scalar) |
| - | Subtraction (matrix and scalar) |
| * | Multiplication (matrix and scalar) |
| .* | Elementwise array multiplication |
| / | Division (matrix and scalar) |
| ./ | Elementwise array division |
| ^ | Exponentiation (matrix and scalar) |
| .^ | Elementwise array exponentiation |
| [ ] | Square brackets, used to enter matrices and vectors |
| ' (single quote) | Matrix/vector transpose or quotes to enclose text |
| : (colon) | Used to generate vectors and pick out matrix rows/columns |
| ; (semicolon) | Used as row delimiter and to supress printing |
| % | Comment |
The name "Matlab" stands for matrix laboratory and consequently (almost) all quantities in Matlab are considered to be matrices or vectors.
To enter a row vector u type
u = [0 1 2 3 4 5]
at the Matlab command prompt. Matlab will immediately print the result in the workspace. To suppress this printing enter u as
u = [0 1 2 3 4 5];
i.e., with a trailing semicolon. To enter the same vector, but as a column vector v, type
v = [0;1;2;3;4;5];
Here the first five semicolons act as row delimiters and the last semicolon suppresses the printout of v. You could also enter v as
v = [0 1 2 3 4 5]';
i.e., as a row vector that is transposed (single quote after ]). To enter a matrix A, e.g., with 2 rows and 3 columns, you type
A = [1 2 3;4 5 6];
To pick out an eement from A, e.g., the first element of the second row you use A(2,1). To pick out the whole first row type A(1,:), to pick out the whole second column use A(:,2). Note that Matlab indexes all vectors and matrices beginning with index 1 (as opposed to index 0 as often used in mathematics). To determine the size of a matrix or vector A type
size(A)
Similarly, the length of a matrix or vector A is obtained from
length(A)
To fill a m×n with all zeros or ones use the commands
zeros(m,n); ones(m,n);
respectively. You can also use zeros(size(A)) to make an all zero matrix of the same size as A. To fill a vector with a consecutive set of numbers, e.g., u1 = [0 1 2 3 ... 10] or u2 = [0 0.1 0.2 ... 1.0] or u3 = [50 49 48 ... 40] use the commands
u1 = [0:10]; u2 = [0:0.1:1]; u3 = [50:-1:40];
respectively. Two vectors or matrices A and B with the same number of rows can be concatenated using
C = [A B]
Similarly, two vectors or matrices A and B with the same number of columns can be concatenated using
D = [A;B]
== To be completed ==
== To be completed ==
== To be completed ==
== To be completed ==
©2003-2007, P. Mathys. Last revised: 2-13-07, PM.