Publication date: 07/08/2024

Matrices and Data Tables

Using JSL, you can move information between a matrix and a JMP data table. You can use matrix algebra to perform calculations on numbers that are stored in JMP data tables, and you can save the results back to JMP data tables.

Move Data into a Matrix from a Data Table

These sections describe how to use JSL to move data from a data table into a matrix.

Move All Numeric Values

The Get As Matrix() function generates a matrix containing all of the numeric values in a data table or column:

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
A = dt << Get As Matrix;

[12 59 95,

12 61 123,

12 55 74, ...]

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
col = Column( "Height" );
A = col << Get As Matrix;

[59, 61, 55, 66, 52, ...]

Move All Numeric Values and Character Columns

The Get All Columns As Matrix() function returns the values from all columns of the data table in a matrix, including character columns. Character columns are numbered according to the alphanumeric order, starting at 1.

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
A = dt << Get All Columns As Matrix;

[21 12 1 59 95,

28 12 1 61 123, ...]

Move Only Certain Columns

To get certain columns of a data table, use column list arguments (names or characters).

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
x = dt << Get As Matrix( {"height", "weight"} );

or

x = dt << Get As Matrix( {height, weight} );

[59 95, 61 123, 55 74, ...]

Currently Selected Rows

To get a matrix of the currently selected row numbers in the data table:

dt << Get Selected Rows;

Note: If no rows are selected, the output is an empty matrix.

Find Rows Where

To see a matrix of row numbers where the expression is true:

dt << Get Rows Where( expression );

For example, the following script returns the row numbers where the sex is male (M):

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
A = dt << Get Rows Where( :sex == "M" );

[6, 7, 8, 12, 13, 14, ...]

or

Where( :sex == "M" );

[6, 7, 8, 12, 13, 14, ...]

Move Data into a Data Table from a Matrix

This section describes how to move data from a matrix into a data table.

Move a Column Vector

The Set Values() function copies values from a column vector into an existing data table column:

col << Set Values( x );

col is a reference to the data table column, and x is a column vector.

For example, the following script creates a new column called test and copies the values of vector x into the test column:

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
dt << New Column( "test" );
col = Column( "test" );
x = 1::40;
col << Set Values( x );

Move All Matrix Values

The Set Matrix() function copies values from a matrix into an existing data table, making new rows and new columns as needed to store the values of the matrix. The new columns are named Col1, Col2, and so on.

dt = New Table( "B" );
dt << Set Matrix([1 2 3 4 5, 6 7 8 9 10]);

This script creates a new data table called B containing two rows and five columns.

To create a new data table from a matrix argument, use the As Table(matrix) command. The columns are named Col1, Col2, and so on. For example, the following script creates a new data table containing the values of A:

A = [1 2 3, 4 5 6, 7 8 9, 10 11 12];
dt = As Table( A );
Want more information? Have questions? Get answers in the JMP User Community (community.jmp.com).