Matrix Functions (MATRIX-END MATRIX command)

The following functions are available in the matrix program. Except where noted, each takes one or more numeric matrices as arguments and returns a matrix value as its result. The arguments must be enclosed in parentheses, and multiple arguments must be separated by commas.

On the following list, matrix arguments are represented by names beginning with M. Unless otherwise noted, these arguments can be vectors or scalars. Arguments that must be vectors are represented by names beginning with V, and arguments that must be scalars are represented by names beginning with S.

ABS(M). Absolute value. Takes a single argument. Returns a matrix having the same dimensions as the argument, containing the absolute values of its elements.

ALL(M). Test for all elements nonzero. Takes a single argument. Returns a scalar: 1 if all elements of the argument are nonzero and 0 if any element is zero.

ANY(M). Test for any element nonzero. Takes a single argument. Returns a scalar: 1 if any element of the argument is nonzero and 0 if all elements are zero.

ARSIN(M). Inverse sine. Takes a single argument, whose elements must be between −1 and 1. Returns a matrix having the same dimensions as the argument, containing the inverse sines (arcsines) of its elements. The results are in radians and are in the range from −π/2 to π/2.

ARTAN(M). Inverse tangent. Takes a single argument. Returns a matrix having the same dimensions as the argument, containing the inverse tangents (arctangents) of its elements, in radians. To convert radians to degrees, multiply by 180/π, which you can compute as 45/ARTAN(1). For example, the statement COMPUTE DEGREES=ARTAN(M)*45/ARTAN(1) returns a matrix containing inverse tangents in degrees.

BLOCK(M1,M2,...). Create a block diagonal matrix. Takes any number of arguments. Returns a matrix with as many rows as the sum of the rows in all the arguments, and as many columns as the sum of the columns in all the arguments, with the argument matrices down the diagonal and zeros elsewhere.

CHOL(M). Cholesky decomposition. Takes a single argument, which must be a symmetric positive-definite matrix (a square matrix, symmetric about the main diagonal, with positive eigenvalues). Returns a matrix having the same dimensions as the argument. If M is a symmetric positive-definite matrix and B=CHOL(M), then T(B)* B=M, where T is the transpose function defined below.

CMAX(M). Column maxima. Takes a single argument. Returns a row vector with the same number of columns as the argument. Each column of the result contains the maximum value of the corresponding column of the argument.

CMIN(M). Column minima. Takes a single argument. Returns a row vector with the same number of columns as the argument. Each column of the result contains the minimum value of the corresponding column of the argument.

COS(M). Cosines. Takes a single argument. Returns a matrix having the same dimensions as the argument, containing the cosines of the elements of the argument. Elements of the argument matrix are assumed to be measured in radians. To convert degrees to radians, multiply by π/180, which you can compute as ARTAN(1)/45. For example, the statement COMPUTE COSINES=COS(DEGREES*ARTAN(1)/45) returns cosines from a matrix containing elements measured in degrees.

CSSQ(M). Column sums of squares. Takes a single argument. Returns a row vector with the same number of columns as the argument. Each column of the result contains the sum of the squared values of the elements in the corresponding column of the argument.

CSUM(M). Column sums. Takes a single argument. Returns a row vector with the same number of columns as the argument. Each column of the result contains the sum of the elements in the corresponding column of the argument.

DESIGN(M). Main-effects design matrix from the columns of a matrix. Takes a single argument. Returns a matrix having the same number of rows as the argument, and as many columns as the sum of the numbers of unique values in each column of the argument. Constant columns in the argument are skipped with a warning message. The result contains 1 in the row(s) where the value in question occurs in the argument and 0 otherwise.

DET(M). Determinant. Takes a single argument, which must be a square matrix. Returns a scalar, which is the determinant of the argument.

DIAG(M). Diagonal of a matrix. Takes a single argument. Returns a column vector with as many rows as the minimum of the number of rows and the number of columns in the argument. The ith element of the result is the value in row i, column i of the argument.

EOF(file). End of file indicator. Normally used after a READ statement. Takes a single argument, which must be either a filename in quotes, or a file handle defined on a FILE HANDLE command that precedes the matrix program. Returns a scalar equal to 1 if the last attempt to read that file encountered the last record in the file, and equal to 0 if the last attempt did not encounter the last record in the file. Calling the EOF function causes a REREAD specification on the READ statement to be ignored on the next attempt to read the file.

EVAL(M). Eigenvalues of a symmetric matrix. Takes a single argument, which must be a symmetric matrix. Returns a column vector with the same number of rows as the argument, containing the eigenvalues of the argument in decreasing numerical order.

EXP(M). Exponentials of matrix elements. Takes a single argument. Returns a matrix having the same dimensions as the argument, in which each element equals e raised to the power of the corresponding element in the argument matrix.

GINV(M). Moore-Penrose generalized inverse of a matrix. Takes a single argument. Returns a matrix with the same dimensions as the transpose of the argument. If A is the generalized inverse of a matrix M, then M*A*M=M and A*M*A=A. Both A*M and M*A are symmetric.

GRADE(M). Ranks elements in a matrix. Takes a single argument. Uses sequential integers for ties.

GSCH(M). Gram-Schmidt orthonormal basis for the space spanned by the column vectors of a matrix. Takes a single argument, in which there must be as many linearly independent columns as there are rows. (That is, the rank of the argument must equal the number of rows.) Returns a square matrix with as many rows as the argument. The columns of the result form a basis for the space spanned by the columns of the argument.

IDENT(S1 [,S2]). Create an identity matrix. Takes either one or two arguments, which must be scalars. Returns a matrix with as many rows as the first argument and as many columns as the second argument, if any. If the second argument is omitted, the result is a square matrix. Elements on the main diagonal of the result equal 1, and all other elements equal 0.

INV(M). Inverse of a matrix. Takes a single argument, which must be square and nonsingular (that is, its determinant must not be 0). Returns a square matrix having the same dimensions as the argument. If A is the inverse of M, then M*A=A*M=I, where I is the identity matrix.

KRONEKER(M1,M2).

A(1,1)*B A(1,2)* B ... A(1,N)*B

A(2,1)*B A(2,2)* B ... A(2,N)* B

...

A(M,1)*B A(M,2)*B ... A(M, N)*B

LG10(M). Base 10 logarithms of the elements. Takes a single argument, all of whose elements must be positive. Returns a matrix having the same dimensions as the argument, in which each element is the logarithm to base 10 of the corresponding element of the argument.

LN(M). Natural logarithms of the elements. Takes a single argument, all of whose elements must be positive. Returns a matrix having the same dimensions as the argument, in which each element is the logarithm to base e of the corresponding element of the argument.

MAGIC(S). Magic square. Takes a single scalar, which must be 3 or larger, as an argument. Returns a square matrix with S rows and S columns containing the integers from 1 through S 2. All of the row sums and all of the column sums are equal in the result matrix. (The result matrix is only one of several possible magic squares.)

MAKE(S1,S2,S3). Create a matrix, all of whose elements equal a specified value. Takes three scalars as arguments. Returns an S1 × S2 matrix, all of whose elements equal S3.

MDIAG(V). Create a square matrix with a specified main diagonal. Takes a single vector as an argument. Returns a square matrix with as many rows and columns as the dimension of the vector. The elements of the vector appear on the main diagonal of the matrix, and the other matrix elements are all 0.

MMAX(M). Maximum element in a matrix. Takes a single argument. Returns a scalar equal to the numerically largest element in the argument M.

MMIN(M). Minimum element in a matrix. Takes a single argument. Returns a scalar equal to the numerically smallest element in the argument M.

MOD(M,S). Remainders after division by a scalar. Takes two arguments, a matrix and a scalar (which must not be 0). Returns a matrix having the same dimensions as M, each of whose elements is the remainder after the corresponding element of M is divided by S. The sign of each element of the result is the same as the sign of the corresponding element of the matrix argument M.

MSSQ(M). Matrix sum of squares. Takes a single argument. Returns a scalar that equals the sum of the squared values of all of the elements in the argument.

MSUM(M). Matrix sum. Takes a single argument. Returns a scalar that equals the sum of all of the elements in the argument.

NCOL(M). Number of columns in a matrix. Takes a single argument. Returns a scalar that equals the number of columns in the argument.

NROW(M). Number of rows in a matrix. Takes a single argument. Returns a scalar that equals the number of rows in the argument.

RANK(M). Rank of a matrix. Takes a single argument. Returns a scalar that equals the number of linearly independent rows or columns in the argument.

RESHAPE(M,S1,S2). Matrix of different dimensions. Takes three arguments, a matrix and two scalars, whose product must equal the number of elements in the matrix. Returns a matrix whose dimensions are given by the scalar arguments. For example, if M is any matrix with exactly 50 elements, then RESHAPE(M, 5, 10) is a matrix with 5 rows and 10 columns. Elements are assigned to the reshaped matrix in order by row.

RMAX(M). Row maxima. Takes a single argument. Returns a column vector with the same number of rows as the argument. Each row of the result contains the maximum value of the corresponding row of the argument.

RMIN(M). Row minima. Takes a single argument. Returns a column vector with the same number of rows as the argument. Each row of the result contains the minimum value of the corresponding row of the argument.

RND(M). Elements rounded to the nearest integers. Takes a single argument. Returns a matrix having the same dimensions as the argument. Each element of the result equals the corresponding element of the argument rounded to an integer.

RNKORDER(M). Ranking of matrix elements in ascending order. Takes a single argument. Returns a matrix having the same dimensions as the argument M. The smallest element of the argument corresponds to a result element of 1, and the largest element of the argument to a result element equal to the number of elements, except that ties (equal elements in M) are resolved by assigning a rank equal to the arithmetic mean of the applicable ranks.

RSSQ(M). Row sums of squares. Takes a single argument. Returns a column vector having the same number of rows as the argument. Each row of the result contains the sum of the squared values of the elements in the corresponding row of the argument.

RSUM(M). Row sums. Takes a single argument. Returns a column vector having the same number of rows as the argument. Each row of the result contains the sum of the elements in the corresponding row of the argument.

SIN(M). Sines. Takes a single argument. Returns a matrix having the same dimensions as the argument, containing the sines of the elements of the argument. Elements of the argument matrix are assumed to be measured in radians. To convert degrees to radians, multiply by π/180, which you can compute as ARTAN(1)/45. For example, the statement COMPUTE SINES=SIN(DEGREES*ARTAN(1)/45) computes sines from a matrix containing elements measured in degrees.

SOLVE(M1,M2). Solution of systems of linear equations. Takes two arguments, the first of which must be square and nonsingular (its determinant must be nonzero), and the second of which must have the same number of rows as the first. Returns a matrix with the same dimensions as the second argument. If M1*X=M2, then X= SOLVE(M1, M2). In effect, this function sets its result X equal to INV(M1)*M2.

SQRT(M). Square roots of elements. Takes a single argument whose elements must not be negative. Returns a matrix having the same dimensions as the arguments, whose elements are the positive square roots of the corresponding elements of the argument.

SSCP(M). Sums of squares and cross-products. Takes a single argument. Returns a square matrix having as many rows (and columns) as the argument has columns. SSCP ( M ) equals T ( M )*M, where T is the transpose function defined below.

SVAL(M). Singular values of a matrix. Takes a single argument. Returns a column vector containing as many rows as the minimum of the numbers of rows and columns in the argument, containing the singular values of the argument in decreasing numerical order. The singular values of a matrix M are the square roots of the eigenvalues of T ( M )*M, where T is the transpose function discussed below.

SWEEP(M,S).

Sweep transformation of a matrix. Takes two arguments, a matrix and a scalar, which must be less than or equal to both the number of rows and the number of columns of the matrix. In other words, the pivot element of the matrix, which is M(S,S), must exist. Returns a matrix of the same dimensions as M. Suppose that S={ k} and A = SWEEP(M,S). If M(k,k) is not 0, then

A(k,k) = 1/M(k,k)

A(i,k) = −M(i,k)/M(k,k), for i not equal to k

A(k,j) = M(k,j)/M(k,k), for j not equal to k

A(i,j) = (M(i,j)*M(k,k), − M(i,k)*M(k,j))/M(k,k), for i,j not equal to k

and if M(k,k) equals 0, then

A(i,k) = A(k,i) = 0, for all i

A(i,j) = M(i,j), for i,j not equal to k

TRACE(M). Sum of the main diagonal elements. Takes a single argument. Returns a scalar, which equals the sum of the elements on the main diagonal of the argument.

TRANSPOS(M). Transpose of the matrix. Takes a single argument. Returns the transpose of the argument. TRANSPOS can be shortened to T.

TRUNC(M). Truncation of elements to integers. Takes a single argument. Returns a matrix having the same dimensions as the argument, whose elements equal the corresponding elements of the argument truncated to integers.

UNIFORM(S1,S2). Uniformly distributed pseudo-random numbers between 0 and 1. Takes two scalars as arguments. Returns a matrix with the number of rows specified by the first argument and the number of columns specified by the second argument, containing pseudo-random numbers uniformly distributed between 0 and 1. The function respects the traditional versus twister and seed settings for the corresponding generator.