Intel® Fortran Compiler 16.0 User and Reference Guide

MAXLOC

Transformational Intrinsic Function (Generic): Returns the location of the maximum value of all elements in an array, a set of elements in an array, or elements in a specified dimension of an array.

result = MAXLOC (array [, dim] [, mask] [, kind])

array

(Input) Must be an array of type integer, real, or character.

dim

(Input; optional) Must be a scalar integer with a value in the range 1 to n, where n is the rank of array.

mask

(Input; optional) Must be a logical array that is conformable with array.

kind

(Input; optional) Must be a scalar integer initialization expression.

Results

The result is an array of type integer. If kind is present, the kind parameter of the result is that specified by kind; otherwise, the kind parameter of the result is that of default integer. If the processor cannot represent the result value in the kind of the result, the result is undefined.

The following rules apply if dim is omitted:

The following rules apply if dim is specified:

If more than one element has maximum value, the element whose subscripts are returned is the first such element, taken in array element order. If array has size zero, or every element of mask has the value .FALSE., the value of the result is controlled by compiler option assume [no]old_maxminloc, which can set the value of the result to either 1 or 0.

If array is of type character, the comparison is done using the ASCII collating sequence.

The setting of compiler options specifying integer size can affect this function.

Example

The value of MAXLOC((/3, 7, 4, 7/)) is (2), which is the subscript of the location of the first occurrence of the maximum value in the rank-one array.

A is the array

  [  4    0   -3    2 ]
  [  3    1   -2    6 ]
  [ -1   -4    5   -5 ].

MAXLOC (A, MASK=A .LT. 5) has the value (1, 1) because these are the subscripts of the location of the maximum value (4) that is less than 5.

MAXLOC (A, DIM=1) has the value (1, 2, 3, 2). 1 is the subscript of the location of the maximum value (4) in column 1; 2 is the subscript of the location of the maximum value (1) in column 2; and so forth.

MAXLOC (A, DIM=2) has the value (1, 4, 3). 1 is the subscript of the location of the maximum value in row 1; 4 is the subscript of the location of the maximum value in row 2; and so forth.

The following shows another example:

 INTEGER i, maxl(1), max
 INTEGER array(3, 3)
 INTEGER, ALLOCATABLE :: AR1(:)
 ! put values in array
 array = RESHAPE((/7, 9, -1, -2, 5, 0, 3, 6, 9/),     &
                 (/3, 3/))
 ! array is  7 -2 3
 !           9  5 6
 !          -1  0 9
 i = SIZE(SHAPE(array))   ! Get number of dimensions
                          ! in array
 ALLOCATE ( AR1(i))       ! Allocate AR1 to number
                          ! of dimensions in array
 AR1 = MAXLOC (array, MASK = array .LT. 7) ! Get
                          ! the location (subscripts) of
                          ! largest element less than 7
                          ! in array
 !
 ! MASK = array .LT. 7 creates a mask array the same
 ! size and shape as array whose elements are .TRUE. if
 ! the corresponding element in array is less than 7,
 ! and .FALSE. if it is not. This mask causes MAXLOC to
 ! return the index of the element in array with the
 ! greatest value less than 7.
 !
 ! array is  7 -2 3 and MASK=array .LT. 7 is  F T T
 !           9  5 6                           F T T
 !          -1  0 9                           T T F
 ! and AR1 = MAXLOC(array, MASK = array .LT. 7) returns
 ! (2, 3), the location of the element with value 6

 maxl = MAXLOC((/1, 4, 3, 4/))    ! returns 2, the first
                                  ! occurrence of maximum
 END

See Also