Publication date: 07/08/2024

Loc Functions

The Loc(), Loc Nonmissing(), Loc Min(), Loc Max(), and Loc Sorted() functions all return matrices that show positions of certain values in a matrix.

Loc

The Loc() function creates a matrix of positions that locate where A is true (nonzero and nonmissing). In most cases, Where() can be used, and is often faster for large matrices.

A = [0 1 . 3 4 0];
B = [2 0 0 2 5 6];

The following example returns the indices for the values of A that are nonmissing and nonzero.

I = Loc( A );

[2, 4, 5]

or

Where ( A );

[2, 4, 5]

The following example returns the indices for the values of A that are less than the corresponding values of B. Note that the two matrices must have the same number of rows and columns.

I = Loc( A < B );

[1, 5, 6]

The following script replaces all values less than 4 in A with 0.

A = [0 1 0 3 4 0];
A[Loc( A < 4 )] = 0;
Show( A );

A = [0 0 0 0 4 0];

The following script returns an empty matrix:

Loc( [2 3 4 5 6] > 7 );

[](0, 1)

Loc Nonmissing

The Loc Nonmissing() function returns a vector of row numbers in a matrix that do not contain any missing values. For example,

A = [1 2 3, 4 . 6, 7 8 ., 8 7 6];
Loc Nonmissing( A );

[1, 4]

Loc Min and Loc Max

The Loc Min() and Loc Max() functions return the position of the first occurrence of the minimum and maximum elements of a matrix. Elements of a matrix are numbered consecutively, starting in the first row, first column, and proceeding left to right.

A = [1 2 2, 2 4 4, 1 1 1];
B = [6, 12, 9];
Show( Loc Max( A ) );
Show( Loc Min( B ) );

Loc Max(A) = 5;

Loc Min(B) = 1;

Loc Sorted

The Loc Sorted() function is mainly used to identify the interval that a number lies within. The function returns the position of the highest value in A that is less than or equal to the value in B. The resulting vector contains an item for each element in B.

A = [10 20 30 40];
B = [35];
Loc Sorted( A, B );

[3]

A = [10 20 40];
B = [35 5 45 20];
Loc Sorted( A, B );

[2, 1, 3, 2]

Here’s an example that defines a list of strings. Loc Sorted() identifies the string at the seventh interval and puts it in a list.

{"No Fat", "Some Fat", "Low Fat", "Normal Fat", "Medium Fat", "High Fat"}
[Loc Sorted( [0, 1, 5, 10, 20, 25], 7)]

{"Low Fat"}

To convert the returned value to a string, include the subscript [1] at the end of the expression:

{"No Fat", "Some Fat", "Low Fat", "Normal Fat", "Medium Fat", "High Fat"}
[Loc Sorted( [0, 1, 5, 10, 20, 25], 7)][1]

"Low Fat"

Notes:

A must be sorted in ascending order.

The returned values are always 1 or greater. If the element in B is smaller than all of the elements in A, then the function returns a value of 1. If the element in B is greater than all of the elements in A, then the function returns n, where n is the number of elements in A.

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