[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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.
18.1 Finding Elements and Checking Conditions | ||
18.2 Rearranging Matrices | ||
18.3 Special Utility Matrices | ||
18.4 Famous Matrices |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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.
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 ] |
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 10.4 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))
.
xor (x, y)
is true if and only if x or y
is true, but not if both x and y are true.
diff (x)
is the
vector of first differences
x(2) - x(1), ..., x(n) - x(n-1).
If x is a matrix, diff (x)
is the matrix of column
differences along the first non-singleton dimension.
The second argument is optional. If supplied, diff (x,
k)
, where k is a nonnegative integer, returns the
k-th differences. It is possible that k is larger than
then first non-singleton dimension of the matrix. In this case,
diff
continues to take the differences along the next
non-singleton dimension.
The dimension along which to take the difference can be explicitly
stated with the optional variable dim. In this case the
k-th order differences are calculated along this dimension.
In the case where k exceeds size (x, dim)
then an empty matrix is returned.
isinf ([13, Inf, NA, NaN]) => [ 0, 1, 0, 0 ] |
isnan ([13, Inf, NA, NaN]) => [ 0, 0, 0, 1 ] |
finite ([13, Inf, NA, NaN]) => [ 1, 0, 0, 0 ] |
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 ] |
[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.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
fliplr ([1, 2; 3, 4]) => 2 1 4 3 |
Note that fliplr
only workw with 2-D arrays. To flip N-d arrays
use flipdim
instead.
flipud ([1, 2; 3, 4]) => 3 4 1 2 |
Due 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.
flipdim ([1, 2; 3, 4], 2) => 2 1 4 3 |
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) |
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.
Negative values of n rotate the matrix in a clockwise direction. For example,
rotdim ([1, 2; 3, 4], -1, [1, 2]) => 3 1 4 2 |
rotates 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]) |
A = ones (2, 2); B = zeros (2, 2); cat (2, A, B) => ans = 1 1 0 0 1 1 0 0 |
Alternatively, we can concatenate A and B along the second dimension the following way:
[A, B]. |
dim can be larger than the dimensions of the N-d array objects and the result will thus have dim dimensions 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 |
1:ndims(a)
(in any order, but each element must appear just once).
permute
function. The expression
ipermute (permute (a, perm), perm) |
For example,
reshape ([1, 2, 3, 4], 2, 2) => 1 3 2 4 |
Note 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.
x = [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 |
Called with a single argument, shiftdim
, removes the leading
singleton dimensions, returning the number of dimensions removed
in the second output argument ns.
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 |
If x is a matrix, do the same for each column of x. If the optional dim argument is given, operate along this dimension
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 |
If the optional argument dim is given, then the matrix is sorted
along the dimension defined by dim. 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(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 second
column.
a = [1, 2; 2, 3; 3, 1]; [s, i] = sort (a (:, 2)); a (i, :) => 3 1 1 2 2 3 |
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 |
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 (x) > l
, elements from the beginning (end) of
x are removed until a vector of length l is obtained.
If x is a matrix, elements are prepended or removed from each row.
If the optional dim argument is given, then operate along this dimension.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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]) |
The optional argument class, allows eye
to return an array of
the specified type, like
val = zeros (n,m, "uint8") |
For compatibility with MATLAB, calling eye
with no arguments
is equivalent to calling it with an argument of 1.
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) |
The optional argument class, allows ones
to return an array of
the specified type, like
val = ones (n,m, "uint8") |
eye
.
The optional argument class, allows zeros
to return an array of
the specified type, like
val = zeros (n,m, "uint8") |
"seed"
, x)
eye
. In
addition, you can set the seed for the random number generator using the
form
rand ("seed", x) |
where x is a scalar value. If called as
rand ("seed") |
rand
returns the current value of the seed.
"seed"
, x)
eye
. In
addition, you can set the seed for the random number generator using the
form
randn ("seed", x) |
where x is a scalar value. If called as
randn ("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.
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 4.2 Ranges.
The linspace
function always returns a row vector.
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.
warn_neg_dim_as_zero
is nonzero, print a warning
for expressions like
eye (-1) |
The default value is 0.
warn_imag_to_real
is nonzero, a warning is
printed for implicit conversions of complex numbers to real numbers.
The default value is 0.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The following functions return famous matrix forms.
A Hankel matrix formed from an m-vector c, and an n-vector r, has the elements
H(i,j) = c(i+j-1), i+j-1 <= m; H(i,j) = r(i+j-m), otherwise |
H (i, j) = 1 / (i + j - 1) |
(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) |
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.
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) |
A Vandermonde matrix has the form:
c(1)^(n-1) ... c(1)^2 c(1) 1 c(2)^(n-1) ... c(2)^2 c(2) 1 . . . . . . . . . . . . . . . c(n)^(n-1) ... c(n)^2 c(n) 1 |
[ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |