Next: Famous Matrices, Previous: Rearranging Matrices, Up: Matrix Manipulation

— Built-in Function: **eye** (`x`)

— Built-in Function:**eye** (`n, m`)

— Built-in Function:**eye** (`..., class`)

— Built-in Function:

— Built-in Function:

Return 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 vector with two elements,`eye`

uses the values of the elements as the number of rows and columns, respectively. For example,eye (3) => 1 0 0 0 1 0 0 0 1The following expressions all produce the same result:

eye (2) == eye (2, 2) == eye (size ([1, 2; 3, 4])The optional argument

class, allows`eye`

to return an array of the specified type, likeval = zeros (n,m, "uint8")For compatibility with Matlab, calling

`eye`

with no arguments is equivalent to calling it with an argument of 1.

— Built-in Function: **ones** (`x`)

— Built-in Function:**ones** (`n, m`)

— Built-in Function:**ones** (`n, m, k, ...`)

— Built-in Function:**ones** (`..., class`)

— Built-in Function:

— Built-in Function:

— Built-in Function:

Return a matrix or N-dimensional array whose elements are all 1. The arguments are handled the same as the arguments for

`eye`

.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 optional argument

class, allows`ones`

to return an array of the specified type, likeval = ones (n,m, "uint8")

— Built-in Function: **zeros** (`x`)

— Built-in Function:**zeros** (`n, m`)

— Built-in Function:**zeros** (`n, m, k, ...`)

— Built-in Function:**zeros** (`..., class`)

— Built-in Function:

— Built-in Function:

— Built-in Function:

Return a matrix or N-dimensional array whose elements are all 0. The arguments are handled the same as the arguments for

`eye`

.The optional argument

class, allows`zeros`

to return an array of the specified type, likeval = zeros (n,m, "uint8")

— Function File: **repmat** (`A, m, n`)

— Function File:**repmat** (`A, `[`m n`])

— Function File:

Form a block matrix of size

mbyn, with a copy of matrixAas each element. Ifnis not specified, form anmbymblock matrix.

— Loadable Function: **rand** (`x`)

— Loadable Function:**rand** (`n, m`)

— Loadable Function:**rand** (`, x`)

— Loadable Function:

— Loadable Function:

`"seed"`

Return a matrix with random elements uniformly distributed on the interval (0, 1). The arguments are handled the same as the arguments for

`eye`

. In addition, you can set the seed for the random number generator using the formrand ("seed",x)where

xis a scalar value. If called asrand ("seed")

`rand`

returns the current value of the seed.

— Loadable Function: **randn** (`x`)

— Loadable Function:**randn** (`n, m`)

— Loadable Function:**randn** (`, x`)

— Loadable Function:

— Loadable Function:

`"seed"`

Return a matrix with normally distributed random elements. The arguments are handled the same as the arguments for

`eye`

. In addition, you can set the seed for the random number generator using the formrandn ("seed",x)where

xis a scalar value. If called asrandn ("seed")

`randn`

returns the current value of the seed.

The `rand`

and `randn`

functions use separate generators.
This ensures that

rand ("seed", 13); randn ("seed", 13); u = rand (100, 1); n = randn (100, 1);

and

rand ("seed", 13); randn ("seed", 13); u = zeros (100, 1); n = zeros (100, 1); for i = 1:100 u(i) = rand (); n(i) = randn (); end

produce equivalent results.

Normally, `rand`

and `randn`

obtain their initial
seeds 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.

If it is invoked without arguments, `rand`

and `randn`

return a
single element of a random sequence.

The `rand`

and `randn`

functions use 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.

— Function File: **randperm** (`n`)

Return a row vector containing a random permutation of the integers from 1 to

n.

— Built-in Function: **diag** (`v, k`)

Return a diagonal matrix with vector

von diagonalk. The second argument is optional. If it is positive, the vector is placed on thek-th super-diagonal. If it is negative, it is placed on the-k-th sub-diagonal. The default value ofkis 0, and the vector is placed on the main diagonal. For example,diag ([1, 2, 3], 1) => 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.
See Ranges.

— Built-in Function: **linspace** (`base, limit, n`)

Return a row vector with

nlinearly spaced elements betweenbaseandlimit. The number of elements,n, must be greater than 1. Thebaseandlimitare always included in the range. Ifbaseis greater thanlimit, the elements are stored in decreasing order. If the number of points is not specified, a value of 100 is used.The

`linspace`

function always returns a row vector.

— Function File: **logspace** (`base, limit, n`)

Similar to

`linspace`

except that the values are logarithmically spaced from 10^base to 10^limit.If

limitis equal to pi, the points are between 10^base and pi,not10^base and 10^pi, in order to be compatible with the corresponding Matlab function.