Next: Programming in Matlab
Up: Simple calculations and graphs
Previous: Vectorized functions and operators;
An important operator in Matlab is the single quote, which represents the
(conjugate) transpose:
>> A = [1 2;3 4]
A =
1 2
3 4
>> A'
ans =
1 3
2 4
>> B = A + i*.5*A
B =
1.0000 + 0.5000i 2.0000 + 1.0000i
3.0000 + 1.5000i 4.0000 + 2.0000i
>> B'
ans =
1.0000 - 0.5000i 3.0000 - 1.5000i
2.0000 - 1.0000i 4.0000 - 2.0000i
In the rare event that the transpose, rather than the conjugate
transpose, is needed, the ``.'
'' operator is used:
>> B.'
ans =
1.0000 + 0.5000i 3.0000 + 1.5000i
2.0000 + 1.0000i 4.0000 + 2.0000i
(note that '
and .'
are equivalent for matrices with real
entries).
The following commands are frequently useful; more information can be
obtained from the on-line help system.
Creating matrices
- zeros(m,n) creates an matrix of zeros;
- ones(m,n) creates an matrix of ones;
- eye(n) creates the identity matrix;
- diag(v) (assuming v is an n-vector) creates an
diagonal matrix with v on the diagonal.
The commands zeros and ones can be given any number of integer
arguments; with k arguments, they each create a k-dimensional array of the
indicated size.
formatting display and graphics
- format
- format short 3.1416
- format short e 3.1416e+00
- format long 3.14159265358979
- format long e 3.141592653589793e+00
- format compact suppresses extra line feeds (all of the output
in this paper is in compact format).
- xlabel('string'), ylabel('string') label the horizontal and
vertical axes, respectively, in the current plot;
- title('string') add a title to the current plot;
- axis([a b c d]) change the window on the current graph to
;
- grid adds a rectangular grid to the current plot;
- hold on freezes the current plot so that subsequent graphs
will be displayed with the current;
- hold off releases the current plot; the next plot will erase
the current before displaying;
- subplot puts multiple plots in one graphics window.
Miscellaneous
- max(x) returns the largest entry of x, if x is a vector; see
help max for the result when x is a k-dimensional array;
- min(x) analogous to max;
- abs(x) returns an array of the same size as x whose
entries are the magnitudes of the entries of x;
- size(A) returns a vector with the number of rows,
columns, etc. of the k-dimensional array A;
- length(x) returns the ``length'' of the array, i.e.
max(size(A)).
- save fname saves the current variables to the file named fname.mat;
- load fname load the variables from the file named fname.mat;
- quit exits Matlab
Next: Programming in Matlab
Up: Simple calculations and graphs
Previous: Vectorized functions and operators;
Mark S. Gockenbach
Wed Sep 8 10:44:13 EDT 1999