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


Publication date: 04/21/2023

Subscripts in Matrices

In JSL, use the subscript operator ([ ]) to pick out elements or submatrices from matrices. The Subscript() function is usually written as a bracket notation after the matrix to be subscripted, with arguments for rows and columns.

Single Element

The expression A[i,j] extracts the element in row i, column j, returning a scalar number. The equivalent functional form is Subscript(A,i,j).

P = [1 2 3, 4 5 6, 7 8 9];
P[2, 3]; // returns 6
Subscript( P, 2, 3 ); // returns 6

Assign the value that is in the third row and the first column in the matrix A (which is 5) to the variable test.

A = [1 2, 3 4, 5 6];
test = A[3, 1];
Show( test );

test = 5;

Matrix or List Subscripts

To extract a sub-matrix, use matrix or list subscripts. The result is a matrix of the selected rows and columns. The following expressions select the second and third rows with the first and third columns.

P = [1 2 3, 4 5 6, 7 8 9];
P[[2 3],[1 3]]; // matrix subscripts
P[{2, 3},{1, 3}]; // list subscripts

Both of these methods provide the following output:

[4 6,

7 9]

Single Subscripts

A single subscript addresses matrices as if all the rows were connected end-to-end in a single row. This makes the double subscript A[i,j] the same as the single subscript A[(i-1)*ncol(A)+j].

Examples

Q = [2 4 6, 8 10 12, 14 16 18];
Q[8]; // same as Q[3,2]

16

The following examples all return the column vector [10, 14, 18]:

Q = [2 4 6, 8 10 12, 14 16 18];
Q[{5, 7, 9}];
Q[[5 7 9]];
ii = [5 7 9];
Q[ii];
ii = {5, 7, 9};
Q[ii];
Subscript( Q, ii );

This script returns the values 1 through 9 from the matrix P in order:

P = [1 2 3, 4 5 6, 7 8 9];
For( i = 1, i <= 3, i++,
	For( j = 1, j <= 3, j++,
		Show( P[i, j] )
	)
);

Delete Rows and Columns

Deleting rows and columns is accomplished by assigning an empty matrix to that row or column.

A[k, 0] = [];  // deletes the kth row
A[0, k] = [];  // deletes the kth column

Select Whole Rows or Columns

A subscript argument of zero selects all rows or columns.

P = [1 2 3, 4 5 6, 7 8 9];

Select column 2:

P[0, 2];

[2, 5, 8]

Select columns 3 and 2:

P[0, [3, 2]];

[3 2, 6 5, 9 8]

Select row 3:

P[3, 0];

[7 8 9]

Select rows 2 and 3:

P[[2, 3], 0];

[4 5 6, 7 8 9]

Select all columns and rows (the whole matrix):

P[0, 0];

[1 2 3, 4 5 6, 7 8 9]

Assignment through Subscripts

You can change values in matrices using subscripts. The subscripts can be single indices, matrices or lists of indices, or the zero index representing all rows or columns. The number of selected rows and columns for the insertion must either match the dimension of the inserted argument, or the argument can be inserted repeatedly into the indexed positions.

Examples

Change the value in row 2, column 3 to 99:

P = [1 2 3, 4 5 6, 7 8 9];
P[2, 3] = 99;
Show( P );

P=[1 2 3, 4 5 99, 7 8 9]

Change the values in four locations:

P = [1 2 3, 4 5 6, 7 8 9];
P[[1 2], [2 3]] = [66 77, 88 99];
Show( P );

P=[1 66 77, 4 88 99, 7 8 9]

Change three values in one column:

P = [1 2 3, 4 5 6, 7 8 9];
P[0, 2] = [11, 22, 33];
Show( P );

P=[1 11 3, 4 22 6, 7 33 9]

Change three values in one row:

P = [1 2 3, 4 5 6, 7 8 9];
P[3, 0] = [100 102 104];
Show( P );

P=[1 2 3, 4 5 6, 100 102 104]

Change all the values in one row to the same value:

P = [1 2 3, 4 5 6, 7 8 9];
P[2, 0] = 99;
Show( P );

P=[1 2 3, 99 99 99, 7 8 9]

Operator Assignment

You can use operator assignments (such as +=) on matrices or subscripts of matrices. For example, the following statement adds 1 to the i - jth element of the matrix:

P = [1 2 3, 4 5 6, 7 8 9];
P[1, 1] += 1;
Show( P );

P=[2 2 3,

4 5 6,

7 8 9];

 
P[1, 1] += 1;
Show( P );

P=[3 2 3,

4 5 6,7 8 9];

Ranges of Rows or Columns

If you are working with a range of subscripts, use the Index() function or :: to create matrices of ranges.

T1 = 1 :: 3; // create the vector [1 2 3]
T2 = 4 :: 6; // create the vector [4 5 6]
T3 = 7 :: 9; // create the vector [7 8 9]
T = T1 |/ T2 |/ T3; // concatenate the vectors into a single matrix
T[1 :: 3, 2 :: 3]; // refer to rows 1 through 3, columns 2 and 3

[2 3, 5 6, 8 9]

T[Index( 1, 3 ), Index( 2, 3 )]; // equivalent Index function

[2 3, 5 6, 8 9]

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