FINDLOC(3) - [ARRAY:LOCATION] Location of first element of ARRAY identified by MASK along dimension DIM matching a target value
result = findloc (array, value, dim [,mask] [,kind] [,back]) | findloc (array, value [,mask] [,kind] [,back])
function findloc (array, value, dim, mask, kind, back)
type(TYPE(kind=KIND)),intent(in) :: array(..) type(TYPE(kind=KIND)),intent(in) :: value integer(kind=**),intent(in),optional :: dim logical(kind=**),intent(in),optional :: mask(..) integer(kind=**),intent(in),optional :: kind logical(kind=**),intent(in),optional :: back
NOTE: a kind designated as ** may be any supported kind for the type
o ARRAY is an array of any intrinsic type. o VALUE shall be scalar but in type conformance with ARRAY, as specified for the operator == or the operator .EQV.. o DIM an integer corresponding to a dimension of ARRAY. The corresponding actual argument shall not be an optional dummy argument. o MASK is logical and shall be conformable with ARRAY. o KIND a scalar integer initialization expression (ie. a constant) o BACK a logical scalar. o the result is integer of default kind or kind KIND if the KIND argument is present. If DIM does not appear, the result is an array of rank one and of size equal to the rank of ARRAY; otherwise, the result is an array of the same rank and shape as ARRAY reduced by the dimension DIM.
FINDLOC(3) returns the location of the first element of ARRAY identified by MASK along dimension DIM having a value equal to VALUE.
If both ARRAY and VALUE are of type logical, the comparison is performed with the .EQV. operator; otherwise, the comparison is performed with the == operator. If the value of the comparison is .true., that element of ARRAY matches VALUE.
If only one element matches VALUE, that elements subscripts are returned. Otherwise, if more than one element matches VALUE and BACK is absent or present with the value .false., the element whose subscripts are returned is the first such element, taken in array element order. If BACK is present with the value .true., the element whose subscripts are returned is the last such element, taken in array element order.
o ARRAY : shall be an array of intrinsic type. o VALUE : shall be scalar and in type conformance with ARRAY. o DIM : shall be an integer scalar with a value in the range 1 <= DIM <= n, where n is the rank of ARRAY. The corresponding actual argument shall not be an optional dummy argument. o MASK : (optional) shall be of type logical and shall be conformable with ARRAY. o KIND : (optional) shall be a scalar integer initialization expression. o BACK : (optional) shall be a logical scalar.
KIND is present, the kind type parameter is that specified by the value of KIND; otherwise the kind type parameter is that of default integer type. If DIM does not appear, the result is an array of rank one and of size equal to the rank of ARRAY; otherwise, the result is of rank n - 1 and shape
[d1, d2, . . ., dDIM-1, dDIM+1, . . ., dn ]where
[d1, d2, . . ., dn ]is the shape of ARRAY.
o CASE (I): The result of FINDLOC (ARRAY, VALUE) is a rank-one array whose element values are the values of the subscripts of an element of ARRAY whose value matches VALUE. If there is such a value, the ith subscript returned lies in the range 1 to ei, where ei is the extent of the ith dimension of ARRAY. If no elements match VALUE or ARRAY has size zero, all elements of the result are zero. o CASE (II): the result of FINDLOC (ARRAY, VALUE, MASK = MASK) is a rank-one array whose element values are the values of the subscripts of an element of ARRAY, corresponding to a true element of MASK, whose value matches VALUE. If there is such a value, the ith subscript returned lies in the range 1 to ei, where ei is the extent of the ith dimension of ARRAY. If no elements match VALUE, ARRAY has size zero, or every element of MASK has the value false, all elements of the result are zero.
Sample program:
program demo_findloc logical,parameter :: T=.true., F=.false. integer,allocatable :: ibox(:,:) logical,allocatable :: mask(:,:) ! basics ! the first element matching the value is returned AS AN ARRAY call printi(== 6,findloc ([2, 6, 4, 6], value = 6)) call printi(== 6,findloc ([2, 6, 4, 6], value = 6,back=.true.)) ! the first element matching the value is returned AS A SCALAR call printi(== 6,findloc ([2, 6, 4, 6], value = 6,dim=1)) call printi(== 6,findloc ([2, 6, 4, 6], value = 6,back=.true.,dim=1))Results:ibox=reshape([ 0,-5, 7, 7, & 3, 4, -1, 2, & 1, 5, 6, 7] ,shape=[3,4],order=[2,1])
mask=reshape([ T, T, F, T, & T, T, F, T, & T, T, F, T] ,shape=[3,4],order=[2,1])
call printi(array is, ibox ) call printl(mask is, mask ) print *, so for == 7 and back=.false. call printi(so for == 7 the address of the element is, & & findloc (ibox, 7, mask = mask) ) print *, so for == 7 and back=.true. call printi(so for == 7 the address of the element is, & & findloc (ibox, 7, mask = mask, back=.true.) )
print *,This is independent of declared lower bounds for the array
print *, using dim=N ibox=reshape([ 1, 2, -9, & 2, 2, 6 ] ,shape=[2,3],order=[2,1])
call printi(array is, ibox ) ! has the value [2, 1, 0] and call printi(,findloc (ibox, value = 2, dim = 1) ) ! has the value [2, 1]. call printi(,findloc (ibox, value = 2, dim = 2) ) contains ! GENERIC ROUTINES TO PRINT MATRICES subroutine printl(title,a) implicit none !@(#) print small 2d logical scalar, vector, matrix in row-column format character(len=*),intent(in) :: title logical,intent(in) :: a(..)
character(len=*),parameter :: row=(" > [ ",*(l1:,",")) character(len=*),parameter :: all=(" ",*(g0,1x)) logical,allocatable :: b(:,:) integer :: i write(*,all,advance=no)trim(title) ! copy everything to a matrix to keep code simple select rank(a) rank (0); write(*,(a)) (a scalar); b=reshape([a],[1,1]) rank (1); write(*,(a)) (a vector); b=reshape(a,[size(a),1]) rank (2); write(*,(a)) (a matrix); b=a rank default; stop *printl* unexpected rank end select do i=1,size(b,dim=1) write(*,fmt=row,advance=no)b(i,:) write(*,(" ]")) enddo write(*,all) >shape=,shape(a),,rank=,rank(a),,size=,size(a) write(*,*) end subroutine printl
subroutine printi(title,a) implicit none !@(#) print small 2d integer scalar, vector, matrix in row-column format character(len=*),intent(in) :: title integer,intent(in) :: a(..) character(len=*),parameter :: all=(" ",*(g0,1x)) character(len=20) :: row integer,allocatable :: b(:,:) integer :: i write(*,all,advance=no)trim(title) ! copy everything to a matrix to keep code simple select rank(a) rank (0); write(*,(a)) (a scalar); b=reshape([a],[1,1]) rank (1); write(*,(a)) (a vector); b=reshape(a,[size(a),1]) rank (2); write(*,(a)) (a matrix); b=a rank default; stop *printi* unexpected rank end select ! find how many characters to use for integers write(row,(i0))ceiling(log10(max(1.0,real(maxval(abs(b))))))+2 ! use this format to write a row row=(" > [",*(i//trim(row)//:,",")) do i=1,size(b,dim=1) write(*,fmt=row,advance=no)b(i,:) write(*,(" ]")) enddo write(*,all) >shape=,shape(a),,rank=,rank(a),,size=,size(a) write(*,*) end subroutine printi end program demo_findloc
> == 6 (a vector) > > [ 2 ] > >shape= 1 ,rank= 1 ,size= 1 > > == 6 (a vector) > > [ 4 ] > >shape= 1 ,rank= 1 ,size= 1 > > == 6 (a scalar) > > [ 2 ] > >shape= ,rank= 0 ,size= 1 > > == 6 (a scalar) > > [ 4 ] > >shape= ,rank= 0 ,size= 1 > > array is (a matrix) > > [ 0, -5, 7, 7 ] > > [ 3, 4, -1, 2 ] > > [ 1, 5, 6, 7 ] > >shape= 3 4 ,rank= 2 ,size= 12 > > mask is (a matrix) > > [ T,T,F,T ] > > [ T,T,F,T ] > > [ T,T,F,T ] > >shape= 3 4 ,rank= 2 ,size= 12 > > so for == 7 and back=.false. > so for == 7 the address of the element is (a vector) > > [ 1 ] > > [ 4 ] > >shape= 2 ,rank= 1 ,size= 2 > > so for == 7 and back=.true. > so for == 7 the address of the element is (a vector) > > [ 3 ] > > [ 4 ] > >shape= 2 ,rank= 1 ,size= 2 > > This is independent of declared lower bounds for the array > using dim=N > array is (a matrix) > > [ 1, 2, -9 ] > > [ 2, 2, 6 ] > >shape= 2 3 ,rank= 2 ,size= 6 > > (a vector) > > [ 2 ] > > [ 1 ] > > [ 0 ] > >shape= 3 ,rank= 1 ,size= 3 > > (a vector) > > [ 2 ] > > [ 1 ] > >shape= 2 ,rank= 1 ,size= 2 >
Fortran 95
Fortran intrinsic descriptions (license: MIT) @urbanjost
o MAXLOC(3) - Location of the maximum value within an array o MINLOC(3) - Location of the minimum value within an array
Nemo Release 3.1 | findloc (3fortran) | November 02, 2024 |