GNU Octave - Matrix Manipulation
GNU Octave - Matrix Manipulation
Matrix Manipulation
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.
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 ]
To see if any of the elements of a matrix are nonzero, you can use a statement like
any (any (a))
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 it's 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)).
This is useful for implementing functions where arguments can either be scalars or of common size.
http://sunsite.univie.ac.at/textbooks/octave/octave_16.html 1/7
3/4/2019 GNU Octave - Matrix Manipulation
The second argument is optional. If supplied, diff (x, k), where k is a nonnegative integer, returns the k-
th differences.
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,
Rearranging Matrices
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
Return a copy of x with the order of the rows reversed. For example,
rotates the given matrix clockwise by 90 degrees. The following are all equivalent statements:
For example,
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.
For example,
sort ([1, 2; 2, 3; 3, 1])
=> 1 1
2 2
3 3
http://sunsite.univie.ac.at/textbooks/octave/octave_16.html 3/7
3/4/2019 GNU Octave - Matrix Manipulation
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,
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
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,
and
tril (ones (3), 1)
=> 1 1 0
1 1 1
1 1 1
http://sunsite.univie.ac.at/textbooks/octave/octave_16.html 4/7
3/4/2019 GNU Octave - Matrix Manipulation
eye (3)
=> 1 0 0
0 1 0
0 0 1
For compatibility with MATLAB, calling eye with no arguments is equivalent to calling it with an
argument of 1.
If you need to create a matrix whose values are all the same, you should use an expression like
The rand and randn functions use separate generators. This ensures that
and
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.
The functions linspace and logspace make it very easy to create vectors with evenly or logarithmically spaced
elements. See section Ranges.
The linspace function always returns a row vector, regardless of the value of prefer_column_vectors.
http://sunsite.univie.ac.at/textbooks/octave/octave_16.html 6/7
3/4/2019 GNU Octave - Matrix Manipulation
If limit is equal to the points are between not in order to be compatible with the corresponding MATLAB
function.
eye (-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.
Famous Matrices
The following functions return famous matrix forms.
A Hankel matrix formed from an m-vector c, and an n-vector r, has the elements
http://sunsite.univie.ac.at/textbooks/octave/octave_16.html 7/7