C/C++ and Fortran

 View Only

How to get the location of the wanted value in an array

By Archive User posted Thu August 28, 2014 10:42 PM


Originally posted by: ChristineLi

In IBM XL Fortran for Linux, V15.1 and IBM XL Fortran for AIX, V15.1, a new Fortran 2008 intrinsic function has been added to let you inquire the location of the first or last matching of the wanted value in an array along the specified dimension. Function FINDLOC returns the subscript of the element of wanted value using positive integers.
The complete format is FINDLOC(ARRAY, VALUE, DIM, MASK, KIND, BACK) where DIM, MASK, KIND and BACK are optional.
The result indicates the subscript of the location of the masked element of ARRAY whose value matches VALUE.
The detailed information on arguments is as follows:

  • ARRAY stands for the searched array. The array must be of type integer, real, complex, logical, character, or byte.
  • VALUE is the scalar whose value is the one you want, which is of the same type as ARRAY.
  • DIM must be an integer scalar that in the range of 1 and RANK(ARRAY). DIM is optional, however, the search mechanism will be different; that's why there are actually two format, FINDLOC(ARRAY, VALUE, DIM, MASK, KIND, BACK) and FINDLOC(ARRAY, VALUE, MASK, KIND, BACK).
    • If DIM is unspecified, the result is an integer array of rank one and of RANK(ARRAY) size. In other words, at most there will be only one element's subscripts, which is the first matching or last matching depending on BACK argument, will be returned.
    • If DIM is specified, the result is an integer array of rank RANK(ARRAY) -1. In other words, the search is along each vector of the specified dimension. There will be at most n elements found where n equals the extent of the specified dimension. BACK argument determines the first or the last matching element in that vector to be the found one. For the found elements, the subscripts of the rest unspecified dimensions will be returned.
    • If no matching value is found, zero is returned representing the corresponding subscript.
  • MASK is an array used to hide certain elements if you need. It must be of type logical and of the same shape as ARRAY.
  • KIND is an integer scalar indicating kind type parameter.
  • BACK controls the direction in which ARRAY is searched. If it is .FALSE. or absent, the array is searched from the beginning. If it is .TRUE., the array is searched from the end.

To learn more examples, please visit the online product documentation: