Publication date: 05/24/2021

The Identity() function constructs an identity matrix of the dimension that you specify. An identity matrix is a square matrix of zeros except for a diagonal of ones. The only argument specifies the dimension.

Identity( 3 );

[1 0 0,

0 1 0,

0 0 1]

The J() function constructs a matrix with the number of rows and columns that you specify as the first two arguments, whose elements are all the third argument, for example:

J( 3, 4, 5 );

[5 5 5 5,

5 5 5 5,

5 5 5 5]

J( 3, 4, Random Normal() ); // your results will differ

[0.407709113182904 1.67359154091978 1.00412665221308 0.240885679837327,

-0.557848036549455 -0.620833861982722 0.877166783247633 1.50413740148892,

-2.09920574748608 -0.154797501010655 0.0463943433032137 0.064041826393316]

The Diag() function creates a diagonal matrix from a square matrix (having an equal number of rows and columns) or a vector. A diagonal matrix is a square matrix whose nondiagonal elements are zero.

D = [1 -1 1];

`Diag( D );`

[1 0 0,

0 -1 0,

0 0 1]

Diag([1, 2, 3, 4]);

[1 0 0 0,

0 2 0 0,

0 0 3 0,

0 0 0 4]

A = [1 2,3 4];

`f = [5];`

`D = Diag( A, f );`

[1 2 0

3 4 0

0 0 5]

In the third example, at first glance, not all of the nondiagonal elements are zero. Using matrix notation, the matrix can be expressed as follows:

[A 0,

0` f]

Where A and f are the matrices from the example, and 0 is a column vector of zeros.

The VecDiag() function creates a column vector from the diagonal elements of a matrix.

`v = Vec Diag(`

[1 0 0 1, 5 3 7 1, 9 9 8 8, 1 5 4 3]);

[1, 3, 8, 3]

The Vec Quadratic() function calculates the hats in regression that go into the standard errors of prediction or the Mahalanobis or T2 statistics for outlier distances. Vec Quadratic(Sym, X) is equivalent to calculating Vec Diag(X*Sym*X`).The first argument is a symmetric matrix, usually an inverse covariance matrix. The second argument is a rectangular matrix with the same number of columns as the symmetric matrix argument.

The Trace() function returns the sum of the diagonal elements for a square matrix.

D = [0 1 2, 2 1 0, 1 2 0];

Trace( D ); // returns 1

The Index() function generates a row vector of integers from the first argument to the last argument. A double colon :: is the equivalent infix operator.

6::10;

[6 7 8 9 10]

Index( 1, 5 );

[1 2 3 4 5]

The optional increment argument changes the default increment of +1.

Index( 0.1, 0.4, 0.1 );

[0.1, 0.2, 0.3, 0.4]

The increment can also be negative.

Index( 6, 0, -2 );

[6, 4, 2, 0]

The default value of the increment is 1, or -1 if the first argument is higher than the second.

The Shape() function reshapes an existing matrix across rows to be the specified dimensions. The following example changes the 3x4 matrix a into a 12x1 matrix:

a = [1 1 1, 2 2 2, 3 3 3, 4 4 4];

Shape( a, 12, 1 );

[1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]

There are several functions that return a row vector based on summary values for each column in a matrix.

mymatrix = [11 22, 33 44, 55 66];

V Max( mymatrix ); // returns the maximum of each column

[55 66]

V Min( mymatrix ); // returns the minimum of each column

[11 22]

V Mean( mymatrix ); // returns the mean of each column

[33 44]

V Median( mymatrix ); // returns the median of each column

[33 44]

V Quantile( mymatrix, 0.75 ); // returns the 0.75 quantile of each column

[55 66]

V Sum( mymatrix ); // returns the sum of each column

[99 132]

V Std( mymatrix ); // returns the standard deviations of each column

[22 22]

The Design() function creates a matrix of design columns for a vector or list. There is one column for each unique value in the vector or list. The design columns have the elements 0 and 1. For example, x below has values 1, 2, and 3, then the design matrix has a column for 1s, a column for 2s, and a column for 3s. Each row of the matrix has a 1 in the column representing that row’s value. So, the first row (1) has a 1 in the 1s column (the first column) and 0s elsewhere; the second row (2) has a 1 in the 2’s column and 0s elsewhere; and so on.

x = [1, 2, 3, 2, 1];

`Design( x );`

[1 0 0,

0 1 0,

0 0 1,

0 1 0,

1 0 0]

A variation is the Design Nom() or Design F() function, which removes the last column and subtracts it from the others. Therefore, the elements of Design Nom() or Design F() matrices are 0, 1, and –1. And the Design Nom() or Design F() matrix has one less column than the vector or list has unique values. This function makes full-rank versions of design matrices for effects.

x = [1, 2, 3, 2, 1];

`Design Nom( x );`

[1 0, 0 1, -1 -1, 0 1, 1 0]

Design Nom() is further demonstrated in the ANOVA Example.

To facilitate ordinal factor coding, use the Design Ord() function. This function produces a full-rank coding with one less column than the number of unique values in the vector or list. The row for the lowest value in the vector or list is all zeros. Each succeeding value adds an additional 1 to the row of the design matrix.

x = [1, 2, 3, 4, 5, 6];

`Design Ord( x );`

[0 0 0 0 0,

1 0 0 0 0,

1 1 0 0 0,

1 1 1 0 0,

1 1 1 1 0,

1 1 1 1 1]

Design(), Design Nom(), and Design Ord() support a second argument that specifies the levels to be looked up and their order. This feature allows design matrices to be created one row at a time.

• Design( values, levels ) creates a design matrix of indicator columns.

• DesignNom( values, levels ) creates a full-rank design matrix of indicator columns.

Notes:

• The values argument can be a single element or a matrix or list of elements.

• The levels argument can be a list or matrix of levels to be looked up.

• The result has the same number of rows as there are elements in the values argument.

• The result always has the same number of columns as there are items in the levels argument. In the case of Design Nom() and Design Ord(), there is one less column than the number of items in the levels argument.

• If a value is not found, the whole row is zero.

Design( 20, [10 20 30] );

[0 1 0]

Design( 30, [10 20 30] );

[0 0 1]

Design Nom( 20, [10 20 30] );

[0 1]

Design Nom( 30, [10 20 30] );

[-1 -1]

Design Ord( 20, [10 20 30] );

[1 0]

Design( [20, 10, 30, 20], [10 20 30] );

[0 1 0,

1 0 0,

0 0 1,

0 1 0]

Design({"b", "a", "c", "b"}, {"a", "b", "c"});

[0 1 0,

1 0 0,

0 0 1,

0 1 0]

The Direct Product() function finds the direct product (or Kronecker product) of two square matrices. The direct product of a m x m matrix and a n x n matrix is the mn x mn matrix whose elements are the products of numbers, one from A and one from B.

G = [1 2, 3 5];

H = [2 3, 5 7];

`Direct Product( G, H );`

[2 3 4 6,

5 7 10 14,

6 9 10 15,

15 21 25 35]

The H Direct Product() function finds the row-by-row direct product of two matrices with the same number of rows.

`H Direct Product( G, H );`

[2 3 4 6, 15 21 25 35]

HDirect Product() is useful for constructing the design matrix columns for interactions.

` XA = Design Nom( A );`

` XB = Design Nom( B );`

` XAB = HDirect Product( XA, XB );`

X = J( NRow( A ), 1 ) || XA || XB || XAB;

Want more information? Have questions? Get answers in the JMP User Community (community.jmp.com).