There are a number of functions available for checking to see if the elements of a matrix meet some condition, and for rearranging the elements of a matrix. For example, Octave can easily tell you if all the elements of a matrix are finite, or are less than some specified value. Octave can also rotate the elements, extract the upper- or lower-triangular parts, or sort the columns of a matrix.

The functions `any`

and `all`

are useful for determining
whether any or all of the elements of a matrix satisfy some condition.
The `find`

function is also useful in determining which elements of
a matrix meet a specified condition.

@anchor{doc-any}

__Built-in Function:__**any***(*`x`,`dim`)-
For a vector argument, return 1 if any element of the vector is
nonzero.
For a matrix argument, return a row vector of ones and zeros with each element indicating whether any of the elements of the corresponding column of the matrix are nonzero. For example,

any (eye (2, 4)) => [ 1, 1, 0, 0 ]

If the optional argument

`dim`is supplied, work along dimension`dim`. For example,any (eye (2, 4), 2) => [ 1; 1 ]

@anchor{doc-all}

__Built-in Function:__**all***(*`x`,`dim`)-
The function
`all`

behaves like the function`any`

, except that it returns true only if all the elements of a vector, or all the elements along dimension`dim`of a matrix, are nonzero.

Since the comparison operators (see section Comparison Operators) return matrices of ones and zeros, it is easy to test a matrix for many things, not just whether the elements are nonzero. For example,

all (all (rand (5) < 0.9)) => 0

tests a random 5 by 5 matrix to see if all of its elements are less than 0.9.

Note that in conditional contexts (like the test clause of `if`

and
`while`

statements) Octave treats the test as if you had typed
`all (all (condition))`

.

@anchor{doc-xor}

__Mapping Function:__**xor***(*`x`,`y`)-
Return the `exclusive or' of the entries of
`x`and`y`. For boolean expressions`x`and`y`,`xor (`

is true if and only if`x`,`y`)`x`or`y`is true, but not if both`x`and`y`are true.

@anchor{doc-is_duplicate_entry}

__Function File:__**is_duplicate_entry***(*`x`)-
Return non-zero if any entries in
`x`are duplicates of one another.

@anchor{doc-diff}

__Function File:__**diff***(*`x`,`k`)-
If
`x`is a vector of length`n`,`diff (`

is the vector of first differences`x`)`x`(2) -`x`(1), ...,`x`(n) -`x`(n-1).If

`x`is a matrix,`diff (`

is the matrix of column differences.`x`)The second argument is optional. If supplied,

`diff (`

, where`x`,`k`)`k`is a nonnegative integer, returns the`k`-th differences.

@anchor{doc-isinf}

__Mapping Function:__**isinf***(*`x`)-
Return 1 for elements of
`x`that are infinite and zero otherwise. For example,isinf ([13, Inf, NA, NaN]) => [ 0, 1, 0, 0 ]

@anchor{doc-isnan}

__Mapping Function:__**isnan***(*`x`)-
Return 1 for elements of
`x`that are NaN values and zero otherwise. For example,isnan ([13, Inf, NA, NaN]) => [ 0, 0, 0, 1 ]

@anchor{doc-finite}

__Mapping Function:__**finite***(*`x`)-
Return 1 for elements of
`x`that are finite values and zero otherwise. For example,finite ([13, Inf, NaN]) => [ 1, 0, 0 ]

@anchor{doc-find}

__Loadable Function:__**find***(*`x`)-
Return a vector of indices of nonzero elements of a matrix. To obtain a
single index for each matrix element, Octave pretends that the columns
of a matrix form one long vector (like Fortran arrays are stored). For
example,
find (eye (2)) => [ 1; 4 ]

If two outputs are requested,

`find`

returns the row and column indices of nonzero elements of a matrix. For example,[i, j] = find (2 * eye (2)) => i = [ 1; 2 ] => j = [ 1; 2 ]

If three outputs are requested,

`find`

also returns a vector containing the nonzero values. For example,[i, j, v] = find (3 * eye (2)) => i = [ 1; 2 ] => j = [ 1; 2 ] => v = [ 3; 3 ]

@anchor{doc-common_size}

__Function File:__[`err`,`y1`, ...] =**common_size***(*`x1`, ...)-
Determine if all input arguments are either scalar or of common
size. If so,
`err`is zero, and`yi`is a matrix of the common size with all entries equal to`xi`if this is a scalar or`xi`otherwise. If the inputs cannot be brought to a common size, errorcode is 1, and`yi`is`xi`. For example,[errorcode, a, b] = common_size ([1 2; 3 4], 5) => errorcode = 0 => a = [ 1, 2; 3, 4 ] => b = [ 5, 5; 5, 5 ]

This is useful for implementing functions where arguments can either be scalars or of common size.

@anchor{doc-fliplr}

__Function File:__**fliplr***(*`x`)-
Return a copy of
`x`with the order of the columns reversed. For example,fliplr ([1, 2; 3, 4]) => 2 1 4 3

@seealso{flipud and rot90}

@anchor{doc-flipud}

__Function File:__**flipud***(*`x`)-
Return a copy of
`x`with the order of the rows reversed. For example,flipud ([1, 2; 3, 4]) => 3 4 1 2

@seealso{fliplr and rot90}

@anchor{doc-rot90}

__Function File:__**rot90***(*`x`,`n`)-
Return a copy of
`x`with the elements rotated counterclockwise in 90-degree increments. The second argument is optional, and specifies how many 90-degree rotations are to be applied (the default value is 1). Negative values of`n`rotate the matrix in a clockwise direction. For example,rot90 ([1, 2; 3, 4], -1) => 3 1 4 2

rotates the given matrix clockwise by 90 degrees. The following are all equivalent statements:

rot90 ([1, 2; 3, 4], -1) == rot90 ([1, 2; 3, 4], 3) == rot90 ([1, 2; 3, 4], 7)

@seealso{flipud and fliplr}

@anchor{doc-reshape}

__Function File:__**reshape***(*`a`,`m`,`n`)-
Return a matrix with
`m`rows and`n`columns whose elements are taken from the matrix`a`. To decide how to order the elements, Octave pretends that the elements of a matrix are stored in column-major order (like Fortran arrays are stored).For example,

reshape ([1, 2, 3, 4], 2, 2) => 1 3 2 4

If the variable

`do_fortran_indexing`

is nonzero, the`reshape`

function is equivalent toretval = zeros (m, n); retval (:) = a;

but it is somewhat less cryptic to use

`reshape`

instead of the colon operator. Note that the total number of elements in the original matrix must match the total number of elements in the new matrix.

@anchor{doc-shift}

__Function File:__**shift***(*`x`,`b`)-
If
`x`is a vector, perform a circular shift of length`b`of the elements of`x`.If

`x`is a matrix, do the same for each column of`x`.

@anchor{doc-sort}

__Loadable Function:__[`s`,`i`] =**sort***(*`x`)-
Return a copy of
`x`with the elements elements arranged in increasing order. For matrices,`sort`

orders the elements in each column.For example,

sort ([1, 2; 2, 3; 3, 1]) => 1 1 2 2 3 3

The

`sort`

function may also be used to produce a matrix containing the original row indices of the elements in the sorted matrix. For example,[s, i] = sort ([1, 2; 2, 3; 3, 1]) => s = 1 1 2 2 3 3 => i = 1 3 2 1 3 2

Since the `sort`

function does not allow sort keys to be specified,
it can't be used to order the rows of a matrix according to the values
of the elements in various columns(6)
in a single call. Using the second output, however, it is possible to
sort all rows based on the values in a given column. Here's an example
that sorts the rows of a matrix based on the values in the second
column.

a = [1, 2; 2, 3; 3, 1]; [s, i] = sort (a (:, 2)); a (i, :) => 3 1 1 2 2 3

@anchor{doc-tril}

__Function File:__**tril***(*`a`,`k`)__Function File:__**triu***(*`a`,`k`)-
Return a new matrix formed by extracting extract the lower (
`tril`

) or upper (`triu`

) triangular part of the matrix`a`, and setting all other elements to zero. The second argument is optional, and specifies how many diagonals above or below the main diagonal should also be set to zero.The default value of

`k`is zero, so that`triu`

and`tril`

normally include the main diagonal as part of the result matrix.If the value of

`k`is negative, additional elements above (for`tril`

) or below (for`triu`

) the main diagonal are also selected.The absolute value of

`k`must not be greater than the number of sub- or super-diagonals.For example,

tril (ones (3), -1) => 0 0 0 1 0 0 1 1 0

and

tril (ones (3), 1) => 1 1 0 1 1 1 1 1 1

@seealso{triu and diag}

@anchor{doc-vec}

__Function File:__**vec***(*`x`)-
Return the vector obtained by stacking the columns of the matrix
`x`one above the other.

@anchor{doc-vech}

__Function File:__**vech***(*`x`)-
Return the vector obtained by eliminating all supradiagonal elements of
the square matrix
`x`and stacking the result one column above the other.

@anchor{doc-prepad}

__Function File:__**prepad***(*`x`,`l`,`c`)__Function File:__**postpad***(*`x`,`l`,`c`)-
Prepends (appends) the scalar value

`c`to the vector`x`until it is of length`l`. If the third argument is not supplied, a value of 0 is used.If

`length (`

, elements from the beginning (end) of`x`) >`l``x`are removed until a vector of length`l`is obtained.If

`x`is a matrix, elements are prepended or removed from each row.

@anchor{doc-eye}

__Built-in Function:__**eye***(*`x`)__Built-in Function:__**eye***(*`n`,`m`)-
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 1

The following expressions all produce the same result:

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

For compatibility with MATLAB, calling

`eye`

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

@anchor{doc-ones}

__Built-in Function:__**ones***(*`x`)__Built-in Function:__**ones***(*`n`,`m`)-
Return a matrix 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)

@anchor{doc-zeros}

__Built-in Function:__**zeros***(*`x`)__Built-in Function:__**zeros***(*`n`,`m`)-
Return a matrix whose elements are all 0. The arguments are handled
the same as the arguments for
`eye`

.

@anchor{doc-repmat}

__Function File:__**repmat***(*`A`,`m`,`n`)__Function File:__**repmat***(*`A`, [`m``n`])-
Form a block matrix of size
`m`by`n`, with a copy of matrix`A`as each element. If`n`is not specified, form an`m`by`m`block matrix.

@anchor{doc-rand}

__Loadable Function:__**rand***(*`x`)__Loadable Function:__**rand***(*`n`,`m`)__Loadable Function:__**rand***(*`"seed"`

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

`x`is a scalar value. If called asrand ("seed")

`rand`

returns the current value of the seed.

@anchor{doc-randn}

__Loadable Function:__**randn***(*`x`)__Loadable Function:__**randn***(*`n`,`m`)__Loadable Function:__**randn***(*`"seed"`

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

`x`is 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.

@anchor{doc-randperm}

__Function File:__**randperm***(*`n`)-
Return a row vector containing a random permutation of the
integers from 1 to
`n`.

@anchor{doc-diag}

__Built-in Function:__**diag***(*`v`,`k`)-
Return a diagonal matrix with vector
`v`on diagonal`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,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 section Ranges.

@anchor{doc-linspace}

__Built-in Function:__**linspace***(*`base`,`limit`,`n`)-
Return a row vector with
`n`linearly spaced elements between`base`and`limit`. The number of elements,`n`, must be greater than 1. 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

`linspace`

function always returns a row vector, regardless of the value of`prefer_column_vectors`

.

@anchor{doc-logspace}

__Function File:__**logspace***(*`base`,`limit`,`n`)-
Similar to
`linspace`

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

`limit`is equal to pi, the points are between 10^base and pi,*not*10^base and 10^pi, in order to be compatible with the corresponding MATLAB function.

@anchor{doc-treat_neg_dim_as_zero}

__Built-in Variable:__**treat_neg_dim_as_zero**-
If the value of
`treat_neg_dim_as_zero`

is nonzero, expressions likeeye (-1)

produce an empty matrix (i.e., row and column dimensions are zero). Otherwise, an error message is printed and control is returned to the top level. The default value is 0.

@anchor{doc-ok_to_lose_imaginary_part}

__Built-in Variable:__**ok_to_lose_imaginary_part**-
If the value of
`ok_to_lose_imaginary_part`

is nonzero, implicit conversions of complex numbers to real numbers are allowed (for example, by fsolve). If the value is`"warn"`

, the conversion is allowed, but a warning is printed. Otherwise, an error message is printed and control is returned to the top level. The default value is`"warn"`

.

The following functions return famous matrix forms.

@anchor{doc-hankel}

__Function File:__**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 elementsH (i, j) = c (i+j-1), i+j-1 <= m; H (i, j) = r (i+j-m), otherwise

@seealso{vander, sylvester_matrix, hilb, invhilb, and toeplitz}

@anchor{doc-hilb}

__Function File:__**hilb***(*`n`)-
Return the Hilbert matrix of order
`n`. The i, j element of a Hilbert matrix is defined asH (i, j) = 1 / (i + j - 1)

@seealso{hankel, vander, sylvester_matrix, invhilb, and toeplitz}

@anchor{doc-invhilb}

__Function File:__**invhilb***(*`n`)-
Return the inverse of a Hilbert matrix of order
`n`. This can be computed computed exactly using(i+j) /n+i-1\ /n+j-1\ /i+j-2\ 2 A(i,j) = -1 (i+j-1)( )( ) ( ) \ n-j / \ n-i / \ i-2 / = p(i) p(j) / (i+j-1)

where

k /k+n-1\ /n\ p(k) = -1 ( ) ( ) \ k-1 / \k/

The validity of this formula can easily be checked by expanding the binomial coefficients in both formulas as factorials. It can be derived more directly via the theory of Cauchy matrices: see J. W. Demmel, Applied Numerical Linear Algebra, page 92.

Compare this 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.

@seealso{hankel, vander, sylvester_matrix, hilb, and toeplitz}

@anchor{doc-sylvester_matrix}

__Function File:__**sylvester_matrix***(*`k`)- Return the Sylvester matrix of order n = 2^k.

@anchor{doc-toeplitz}

__Function File:__**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

c(0) r(1) r(2) ... r(n) c(1) c(0) r(1) r(n-1) c(2) c(1) c(0) r(n-2) . . . . . . c(n) c(n-1) c(n-2) ... c(0)

@seealso{hankel, vander, sylvester_matrix, hilb, and invhib}

@anchor{doc-vander}

__Function File:__**vander***(*`c`)-
Return the Vandermonde matrix whose next to last column is
`c`.A Vandermonde matrix has the form

c(0)^n ... c(0)^2 c(0) 1 c(1)^n ... c(1)^2 c(1) 1 . . . . . . . . . . . . c(n)^n ... c(n)^2 c(n) 1

@seealso{hankel, sylvester_matrix, hilb, invhilb, and toeplitz}

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