For the latest version of JMP Help, visit JMP.com/help.


Publication date: 05/05/2023

Use Matrices

The matrix concatenation operators, || and |/, add columns or rows to a matrix dynamically. However, the script runs faster if you define a matrix in a static fashion instead.

// faster example

t1 = Tick Seconds();
A = J( 10, 10000, . );
For( i = 1, i <= 10000, i++,
	a2 = J( 10, 1, Random Gamma( 1, 1 ) );

// define a2 to be a 10 by 1 vector of random gamma values.

       A[0, i] = a2;

/* populate the ith column of the matrix A to have the

values in a2. The first argument, 0, means "all rows". */

);
t2 = Tick Seconds();
Show( t2 - t1 );

t2 - t1 = 0.0166666666627862;

 

// slower example

t3 = Tick Seconds();
B = J( 10, 0, . );
For( i = 1, i <= 10000, i++,
	a2 = J( 10, 1, Random Gamma( 1, 1 ) );
	B = B || a2;
);
t4 = Tick Seconds();
Show( t4 - t3 );

t4 - t3 = 3.1333333333605;

If you know the size of a matrix that you're going to need, it’s typically better to allocate the matrix in its entirety first and then populate it. In the example below, the J() function is doing the allocation of the 100000x1 matrix all at the same time.

Delete Symbols( a1, a2 );

// faster example

t1 = Tick Seconds();
a1 = J( 100000, 1, Random Uniform() );
t2 = Tick Seconds();
Show( t2 - t1 );
Delete Symbols( a1 );

t2 - t1 = 0;

 

// slower example

t3 = Tick Seconds();
a2 = J( 0, 0 );
For( i = 1, i <= 100000, i++,
	a2 |/= Random Uniform()
);
t4 = Tick Seconds();
Show( t4 - t3 );

t4 - t3 = 5.05000000004657;

Use matrices rather than a data table to store numbers.

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