Locates the first element or the last element of an array along a dimension that has the maximum value of all elements corresponding to the true values of the mask. MAXLOC will return the index referable to the position of the element using a positive integer.
Transformational function
The result indicates the subscript of the location of the maximum masked element of ARRAY. If ARRAY is of type character, the comparison is done using the ASCII collating sequence. If more than one element is equal to this maximum value and BACK is absent or has the value .FALSE., the function finds the location of the first element in array element order. If BACK is present with the value .TRUE., the function finds the location of the last element in array element order. If DIM is specified, the result indicates the location of the maximum masked element along each vector of the dimension.
The addition of the DIM argument modifies the behavior from XL Fortran Version 3.
! A is the array | 4 9 8 -7 |
! | 2 1 -1 5 |
! | 9 5 -1 9 |
! | -7 5 5 -7 |
RES = MAXLOC(A)
The
result is | 3 1 | because the maximum value, 9, is located at A(3,
1). Although there are other instances of the maximum value present,
A(3, 1) is the first in column-major order.RES = MAXLOC(A, BACK = .TRUE.)
The
result is | 3 4 | because the maximum value, 9, is located at A(3,
4). Although there are other instances of the maximum value present,
A(3, 4) is the last in column-major order.RES = MAXLOC(A, DIM = 1, MASK = A .LT. 7)
The
result is | 1 3 4 2 | because these are the corresponding row locations
of the first largest value that is less than 7 in each column (the
values being 4, 5, 5, 5).RES = MAXLOC(A, DIM = 1, MASK = A .LT. 7, BACK = .TRUE.)
The
result is | 1 4 4 2 | because these are the corresponding row locations
of the last largest value that is less than 7 in each column (the
values being 4, 5, 5, 5). INTEGER B(-100:100)
! Maxloc views the bounds as (1:201)
! If the largest element is located at index '-49'
I = MAXLOC(B)
! Will return the index '52'
! To return the exact index for the largest element, insert:
INDEX = LBOUND(B) - 1 + I
! Which is: INDEX = (-100) - 1 + 52 = (-49)
PRINT*, B(INDEX)