- publishing free software manuals
GNU Octave Manual
by John W. Eaton
Paperback (6"x9"), 324 pages, 4 figures
ISBN 0954161726
RRP £19.99 ($29.99)

Get a printed copy>>>

15.2 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

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

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)

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

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.

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

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

Function File: vec (x)
Return the vector obtained by stacking the columns of the matrix x one above the other.

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.

ISBN 0954161726GNU Octave ManualSee the print edition