Octave provides a number of functions for creating special matrix forms. In nearly all cases, it is best to use the built-in functions for this purpose than to try to use other tricks to achieve the same effect.
eye returns an identity matrix. If invoked with a
single scalar argument,
eye returns a square matrix with the
dimension specified. If you supply two scalar arguments,
takes them to be the number of rows and columns. If given a matrix or
eye returns an identity matrix with the same
dimensions as the given argument.
creates an identity matrix with three rows and three columns,
eye (5, 8)
creates an identity matrix with five rows and eight columns, and
eye ([13, 21; 34, 55])
creates an identity matrix with two rows and two columns.
eye expects any scalar arguments you provide to be real
and non-negative. The variables
treat_neg_dim_as_zero control the behavior of
complex and negative arguments. See section User Preferences. Any
non-integer arguments are rounded to the nearest integer value.
There is an ambiguity when these functions are called with a single argument. You may have intended to create a matrix with the same dimensions as another variable, but ended up with something quite different, because the variable that you used as an argument was a scalar instead of a matrix.
For example, if you need to create an identity matrix with the same dimensions as another variable in your program, it is best to use code like this
eye (rows (a), columns (a))
instead of just
unless you know that the variable a will always be a matrix.
rand all work like
eye, except that they fill the resulting matrix with all ones,
all zeros, or a set of random values.
If you need to create a matrix whose values are all the same, you should use an expression like
val_matrix = val * ones (n, m)
rand function also takes some additional arguments that allow
you to control its behavior. For example, the function call
causes the sequence of numbers to be normally distributed. You may also
use an argument of
"uniform" to select a uniform distribution. To
find out what the current distribution is, use an argument of
rand obtains the seed from the system clock, so that
the sequence of random numbers is not the same each time you run Octave.
If you really do need for to reproduce a sequence of numbers exactly,
you can set the seed to a specific value. For example, the function call
rand ("seed", 13)
sets the seed to the number 13. To see what the current seed is, use
If it is invoked without arguments,
rand returns a
single element of a random sequence.
rand function uses Fortran code from RANLIB, a library
of fortran routines for random number generation, compiled by Barry W.
Brown and James Lovato of the Department of Biomathematics at The
University of Texas, M.D. Anderson Cancer Center, Houston, TX 77030.
To create a diagonal matrix with vector v on diagonal k, use the function diag (v, k). The second argument is optional. If it is positive, the vector is placed on the k-th super-diagonal. If it is negative, it is placed on the -k-th sub-diagonal. The default value of k is 0, and the vector is placed on the main diagonal. For example,
octave:13> diag ([1, 2, 3], 1) ans = 0 1 0 0 0 0 2 0 0 0 0 3 0 0 0 0
logspace make it very easy to
create vectors with evenly or logarithmically spaced elements. For
linspace (base, limit, n)
creates a vector with n (n greater than 2) linearly spaced elements between base and limit. The base and limit are always included in the range. If base is greater than limit, the elements are stored in decreasing order. If the number of points is not specified, a value of 100 is used.
logspace is similar to
linspace except that
the values are logarithmically spaced.
If limit is equal to the points are between not in order to be compatible with the corresponding MATLAB function.
The following functions return famous matrix forms.
hankel (c, r)
inverse (hilb (n)), which suffers from the ill-conditioning of the Hilbert matrix, and the finite precision of your computer's floating point arithmetic.
toeplitz (c, r)