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

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.

Finding Elements and Checking Conditions

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.

Given a vector, the function any returns 1 if any element of the vector is nonzero.

For a matrix argument, any returns 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,

octave:13> any (eye (2, 4))
ans =

  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))

For a matrix argument, any returns 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.

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 in a column 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,

octave:13> all (all (rand (5) < 0.9))
ans = 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)).

The functions isinf, finite, and isnan return 1 if their arguments are infinite, finite, or not a number, respectively, and return 0 otherwise. For matrix values, they all work on an element by element basis. For example, evaluating the expression

isinf ([1, 2; Inf, 4])

produces the matrix

ans =

  0  0
  1  0

The function find returns 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,

octave:13> find (eye (2))
ans =

  1
  4

If two outputs are requested, find returns the row and column indices of nonzero elements of a matrix. For example,

octave:13> [i, j] = find (eye (2))
i =

  1
  2

j =

  1
  2

If three outputs are requested, find also returns the nonzero values in a vector.

Rearranging Matrices

The function fliplr reverses the order of the columns in a matrix, and flipud reverses the order of the rows. For example,

octave:13> fliplr ([1, 2; 3, 4])
ans =

  2  1
  4  3

octave:13> flipud ([1, 2; 3, 4])
ans =

  3  4
  1  2

The function rot90 (a, n) rotates a matrix 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)
ans =

  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)

The function reshape (a, m, n) returns 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,

octave:13> reshape ([1, 2, 3, 4], 2, 2)
ans =

  1  3
  2  4

If the variable do_fortran_indexing is "true", the reshape function is equivalent to

retval = 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.

The function `sort' can be used to arrange the elements of a vector in increasing order. For matrices, sort orders the elements in each column.

For example,

octave:13> sort (rand (4))
ans =

  0.065359  0.039391  0.376076  0.384298
  0.111486  0.140872  0.418035  0.824459
  0.269991  0.274446  0.421374  0.938918
  0.580030  0.975784  0.562145  0.954964

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 =

  0.051724  0.485904  0.253614  0.348008
  0.391608  0.526686  0.536952  0.600317
  0.733534  0.545522  0.691719  0.636974
  0.986353  0.976130  0.868598  0.713884

i =

  2  4  2  3
  4  1  3  4
  1  2  4  1
  3  3  1  2

These values may be used to recover the original matrix from the sorted version. For example,



The sort function does not allow sort keys to be specified, so it can't be used to order the rows of a matrix according to the values of the elements in various columns(5) 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 third column.

octave:13> a = rand (4) 
a =

  0.080606  0.453558  0.835597  0.437013
  0.277233  0.625541  0.447317  0.952203
  0.569785  0.528797  0.319433  0.747698
  0.385467  0.124427  0.883673  0.226632

octave:14> [s, i] = sort (a (:, 3));
octave:15> a (i, :)
ans =

  0.569785  0.528797  0.319433  0.747698
  0.277233  0.625541  0.447317  0.952203
  0.080606  0.453558  0.835597  0.437013
  0.385467  0.124427  0.883673  0.226632

The functions triu (a, k) and tril (a, k) extract the upper or lower triangular part of the matrix a, and set 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,

octave:13> tril (rand (4), 1)
ans =

  0.00000  0.00000  0.00000  0.00000
  0.09012  0.00000  0.00000  0.00000
  0.01215  0.34768  0.00000  0.00000
  0.00302  0.69518  0.91940  0.00000

forms a lower triangular matrix from a random 4 by 4 matrix, omitting the main diagonal, and

octave:13> tril (rand (4), -1)
ans =

  0.06170  0.51396  0.00000  0.00000
  0.96199  0.11986  0.35714  0.00000
  0.16185  0.61442  0.79343  0.52029
  0.68016  0.48835  0.63609  0.72113

forms a lower triangular matrix from a random 4 by 4 matrix, including the main diagonal and the first super-diagonal.


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