Locates the first element, or the last element if BACK is .TRUE., of an array along a dimension that has the minimum value of all elements corresponding to the true values of the mask. MINLOC 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 minimum 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 minimum 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 minimum 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 = MINLOC(A)
The
result is | 4 1 | because the minimum value, -7, is located at A(4,
1). Although there are other instances of the minimum value present,
A(4, 1) is the first in column-major order.RES = MINLOC(A, BACK = .TRUE.)
The
result is | 4 4 | because the minimum value, -7, is located at A(4,
4). Although there are other instances of the minimum value present,
A(4, 4) is the last in column-major order.RES = MINLOC(A, DIM = 2, MASK = A .NE. -7)
The
result is | 1 3 3 2 | because these are the corresponding column locations
of the first smallest value that is not equal to -7 in each row (the
values being 4, -1, -1, 5).RES = MINLOC(A, DIM = 2, MASK = A .NE. -7, BACK = .TRUE.)
The
result is | 1 3 3 3 | because these are the corresponding column locations
of the last smallest value that is not equal to -7 in each row (the
values being 4, -1, -1, 5). INTEGER B(-100:100)
! Minloc views the bounds as (1:201)
! If the smallest element is located at index '-49'
I = MINLOC(B)
! Will return the index '52'
! To return the exact index for the smallest element, insert:
INDEX = LBOUND(B) - 1 + I
! Which is: INDEX = (-100) - 1 + 52 = (-49)
PRINT*, B(INDEX)