Bellow, FORTRAN CODE, , matlab scilab. () , 22 ()
PROGRAM Principal
REAL*8 A(8)
INTEGER n, npos, pos(8)
n=8
A = (/ 19, 20, 21, 22, 23, 24, 25, 26 /)
CALL FindInVector(n,A==22,npos,pos)
WRITE(*,*) pos(1:npos)
CALL FindInVector(n,ABS(A/2.d0-INT(A/2.d0))<1.d-2,npos,pos)
WRITE(*,*) pos(1:npos)
END PROGRAM Principal
! ________________________________________________________________
! , TF (True False). npos .
SUBROUTINE FindInVector(n,TF,npos,pos)
INTEGER,INTENT(IN):: n ! Dimension of logical vector
LOGICAL,INTENT(IN):: TF(n) ! Logical vector (True or False)
INTEGER npos ! number of "true" conditions
INTEGER pos(n) ! position of "true" conditions
INTEGER i ! counter
INTEGER v(n) ! vector of all positions
pos = 0 ! Initialize pos
FORALL(i=1:n) v(i) = i ! Enumerate all positions
npos = COUNT(TF) ! Count the elements of TF that are .True.
pos(1:npos)= pack(v, TF) ! With Pack function, verify position of true conditions
ENDSUBROUTINE FindInVector