imax

Syntax

imax(X)

Details

Returns the position of the element with the largest value in X. If there are multiple identical maximum values, the first maximum value starting from the left is returned.

The indexing starts from 0.

If the input is an empty vector, -1 is returned.

Parameters

X is a scalar/vector/matrix/table.

Returns

  • If X is a scalar or vector, return an INT scalar, indicating the position of the element with the largest value.
  • If X is a matrix, return a vector containing the position of the element with the largest value in each column.

  • If X is a table, return a table. Each column of the table contains the position of the element with the largest value in the corresponding column of X.

Examples

x = 1.2 2 NULL 6 -1 6;
imax(x);
// output: 3

x = 5 3 1 6 4 6 $ 3:2;
imax(x);
// output: (0,1)

x=array(int,0);
x;
// output: []

imax(x);
// output: -1
// for an empty vector, imax returns -1.

m=matrix(1 2 3, 6 5 4);
m;
#0 #1
1 6
2 5
3 4
imax(m);
// output: [2,0]