Next: Special Utility Matrices, Previous: Finding Elements and Checking Conditions, Up: Matrix Manipulation

— Function File: **fliplr** (`x`)

Return a copy of

xwith the order of the columns reversed. For example,fliplr ([1, 2; 3, 4]) => 2 1 4 3Note that

`fliplr`

only workw with 2-D arrays. To flip N-d arrays use`flipdim`

instead.

— Function File: **flipud** (`x`)

Return a copy of

xwith the order of the rows reversed. For example,flipud ([1, 2; 3, 4]) => 3 4 1 2Due to the difficulty of defining which axis about which to flip the matrix

`flipud`

only work with 2-d arrays. To flip N-d arrays use`flipdim`

instead.

— Function File: **flipdim** (`x, dim`)

Return a copy of

xflipped about the dimensiondim. For exampleflipdim ([1, 2; 3, 4], 2) => 2 1 4 3

— Function File: **rot90** (`x, n`)

Return a copy of

xwith 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 ofnrotate the matrix in a clockwise direction. For example,rot90 ([1, 2; 3, 4], -1) => 3 1 4 2rotates 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)Due to the difficulty of defining an axis about which to rotate the matrix

`rot90`

only work with 2-D arrays. To rotate N-d arrays use`rotdim`

instead.

— Function File: **rotdim** (`x, n, plane`)

Return a copy of

xwith 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). The third argument is also optional and defines the plane of the rotation. As suchplaneis a two element vector containing two different valid dimensions of the matrix. Ifplaneis not given Then the first two non-singleton dimensions are used.Negative values of

nrotate the matrix in a clockwise direction. For example,rotdim ([1, 2; 3, 4], -1, [1, 2]) => 3 1 4 2rotates the given matrix clockwise by 90 degrees. The following are all equivalent statements:

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

— Built-in Function: **cat** (`dim, array1, array2, ..., arrayN`)

Return the concatenation of N-d array objects,

array1,array2, ...,arrayNalong dimensiondim.A = ones (2, 2); B = zeros (2, 2); cat (2, A, B) => ans = 1 1 0 0 1 1 0 0Alternatively, we can concatenate

AandBalong the second dimension the following way:[A, B].

dimcan be larger than the dimensions of the N-d array objects and the result will thus havedimdimensions as the following example shows:cat (4, ones(2, 2), zeros (2, 2)) => ans = ans(:,:,1,1) = 1 1 1 1 ans(:,:,1,2) = 0 0 0 0

— Built-in Function: **horzcat** (`array1, array2, ..., arrayN`)

Return the horizontal concatenation of N-d array objects,

array1,array2, ...,arrayNalong dimension 2.

— Built-in Function: **vertcat** (`array1, array2, ..., arrayN`)

Return the vertical concatenation of N-d array objects,

array1,array2, ...,arrayNalong dimension 1.

— Built-in Function: **permute** (`a, perm`)

Return the generalized transpose for an N-d array object

a. The permutation vectorpermmust contain the elements`1:ndims(a)`

(in any order, but each element must appear just once).

— Built-in Function: **ipermute** (`a, iperm`)

The inverse of the

`permute`

function. The expressionipermute (permute (a, perm), perm)returns the original array

a.

— Function File: **reshape** (`a, m, n, ...`)

— Function File:**reshape** (`a, siz`)

— Function File:

Return a matrix with the given dimensions whose elements are taken from the matrix

a. The elements of the matrix are access in column-major order (like Fortran arrays are stored).For example,

reshape ([1, 2, 3, 4], 2, 2) => 1 3 2 4Note that the total number of elements in the original matrix must match the total number of elements in the new matrix.

A single dimension of the return matrix can be unknown and is flagged by an empty argument.

— Function File: `y` **=**` circshift `(`x, n`)

Circularly shifts the values of the array

x.nmust be a vector of integers no longer than the number of dimensions inx. The values ofncan be either positive or negative, which determines the direction in which the values orxare shifted. If an element ofnis zero, then the corresponding dimension ofxwill not be shifted. For examplex = [1, 2, 3; 4, 5, 6, 7, 8, 9]; circshift (x, 1) => 7, 8, 9 1, 2, 3 4, 5, 6 circshift (x, -2) => 7, 8, 9 1, 2, 3 4, 5, 6 circshift (x, [0,1]) => 3, 1, 2 6, 4, 5 9, 7, 8

— Function File: `y` **=**` shiftdim `(`x, n`)

— Function File: [`y`, `ns`] **=**` shiftdim `(`x`)

— Function File: [

Shifts the dimension of

xbyn, wherenmust be an integer scalar. Whennis positive, the dimensions ofxare shifted to the left, with the leading dimensions circulated to the end. Ifnis negative, then the dimensions ofxare shifted to the right, withnleading singleton dimensions added.Called with a single argument,

`shiftdim`

, removes the leading singleton dimensions, returning the number of dimensions removed in the second output argumentns.For example

x = ones (1, 2, 3); size (shiftdim (x, -1)) => [2, 3, 1] size (shiftdim (x, 1)) => [1, 1, 2, 3] [b, ns] = shiftdim (x); => b = [1, 1, 1; 1, 1, 1] => ns = 1

— Function File: **shift** (`x, b`)

— Function File:**shift** (`x, b, dim`)

— Function File:

If

xis a vector, perform a circular shift of lengthbof the elements ofx.If

xis a matrix, do the same for each column ofx. If the optionaldimargument is given, operate along this dimension

— Loadable Function: [`s`, `i`] = **sort** (`x`)

— Loadable Function: [`s`, `i`] = **sort** (`x, dim`)

— Loadable Function: [`s`, `i`] = **sort** (`x, mode`)

— Loadable Function: [`s`, `i`] = **sort** (`x, dim, mode`)

— Loadable Function: [

— Loadable Function: [

— Loadable Function: [

Return a copy of

xwith 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 3The

`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 2If the optional argument

dimis given, then the matrix is sorted along the dimension defined bydim. The optional argument`mode`

defines the order in which the values will be sorted. Valid values of`mode`

are `ascend' or `descend'.For equal elements, the indices are such that the equal elements are listed in the order that appeared in the original list.

The

`sort`

function may also be used to sort strings and cell arrays of strings, it which case the dictionary order of the strings is used.The algorithm used in

`sort`

is optimized for the sorting of partially ordered lists.

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^{1}
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`)

— Function File:

Return a new matrix formed by extracting extract the lower (

`tril`

) or upper (`triu`

) triangular part of the matrixa, 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

kis zero, so that`triu`

and`tril`

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

kis negative, additional elements above (for`tril`

) or below (for`triu`

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

kmust 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 0and

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

xone above the other.

— Function File: **vech** (`x`)

Return the vector obtained by eliminating all supradiagonal elements of the square matrix

xand stacking the result one column above the other.

— Function File: **prepad** (`x, l, c`)

— Function File:**postpad** (`x, l, c`)

— Function File:**postpad** (`x, l, c, dim`)

— Function File:

— Function File:

Prepends (appends) the scalar value

cto the vectorxuntil it is of lengthl. If the third argument is not supplied, a value of 0 is used.If

`length (`

x`) >`

l, elements from the beginning (end) ofxare removed until a vector of lengthlis obtained.If

xis a matrix, elements are prepended or removed from each row.If the optional

dimargument is given, then operate along this dimension.

[1] For example, to first sort based on the values in column 1, and then, for any values that are repeated in column 1, sort based on the values found in column 2, etc.