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.

The function `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, `eye`

takes them to be the number of rows and columns. If given a matrix or
vector argument, `eye`

returns an identity matrix with the same
dimensions as the given argument.

For example,

eye (3)

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.

Normally, `eye`

expects any scalar arguments you provide to be real
and non-negative. The variables `ok_to_lose_imaginary_part`

and
`treat_neg_dim_as_zero`

control the behavior of `eye`

for
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

eye (a)

unless you know that the variable `a` will *always* be a matrix.

The functions `ones`

, `zeros`

, and `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)

The `rand`

function also takes some additional arguments that allow
you to control its behavior. For example, the function call

rand ("normal")

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
`"dist"`

.

Normally, `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
the argument `"seed"`

.

If it is invoked without arguments, `rand`

returns a
single element of a random sequence.

The `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

The functions `linspace`

and `logspace`

make it very easy to
create vectors with evenly or logarithmically spaced elements. For
example,

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.

The function `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.

`hadamard (`

`k`)- Return the Hadamard matrix of order n = 2^k.
`hankel (`

`c`,`r`)-
Return the Hankel matrix constructed given the first column
`c`, and (optionally) the last row`r`. If the last element of`c`is not the same as the first element of`r`, the last element of`c`is used. If the second argument is omitted, the last row is taken to be the same as the first column. A Hankel matrix formed from an m-vector`c`, and an n-vector`r`, has the elements `hilb (`

`n`)-
Return the Hilbert matrix of order
`n`. The element of a Hilbert matrix is defined as `invhilb (`

`n`)-
Return the inverse of a Hilbert matrix of order
`n`. This is exact. Compare with the numerical calculation of`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`)-
Return the Toeplitz matrix constructed given the first column
`c`, and (optionally) the first row`r`. If the first element of`c`is not the same as the first element of`r`, the first element of`c`is used. If the second argument is omitted, the first row is taken to be the same as the first column. A square Toeplitz matrix has the form `vander (`

`c`)-
Return the Vandermonde matrix whose next to last column is
`c`. A Vandermonde matrix has the form

Go to the first, previous, next, last section, table of contents.