finding specific indices with pointer array

前端 未结 3 1656
北海茫月
北海茫月 2021-01-25 13:24

I am relatively new to Fortran and break my head about one thing for hours now:

I want to write a subroutine for finding the indexes for specific elements in a real 1D

相关标签:
3条回答
  • 2021-01-25 13:29

    A simple way to get the indices of a rank 1 array arr for elements greater than value min is

    indices = PACK([(i, i=LBOUND(arr,1), UBOUND(arr,1))], arr.gt.min)
    

    where indices is allocatable, dimension(:). If your compiler doesn't support automatic (re-)allocation than an ALLOCATE(indices(COUNT(arr.gt.min)) would be needed before that point (with a DEALLOCATE before that if indices is already allocated).

    As explanation: the [(i, i=...)] creates an array with the numbers of the indices of the other array, and the PACK intrinsic selects those corresponding to the mask condition.

    Note that if you are doing index calculations in a subroutine you have to be careful:

    subroutine COMP(arr, min, indices)
      real, intent(in) :: arr(:)
      real, intent(in) :: min
      integer, allocatable, intent(out) :: indices(:)
    
      !...
    end subroutine
    

    arr in the subroutine will have lower bound 1 regardless of the bounds of the actual argument (the array passed) (which could be, say VALS(10:109). You will also then want to pass the lower bound to the subroutine, or address that later.

    [Automatic allocation is not an F90 feature, but in F90 one also has to think about allocatable subroutine arguments

    0 讨论(0)
  • 2021-01-25 13:34

    I think you're on the right track, but you're ignoring some intrinsic Fortran functions, specifically count, and you aren't returning anything!

    subroutine comp(arr, min)
       real, intent(in) :: arr(:)
       real, intent(in) :: min
    ! local variables
       integer, allocatable :: indices(:)
       integer :: i,j, indx
    
    
    ! count counts the number of TRUE elements in the array 
       indx = count(arr > min)
       allocate(indices(indx))
    
    ! the variable j here is the counter to increment the index of indices
       j=1
       do i=1,size(arr)
          if(arr(i) > min) then
             indices(j) = i
             j = j+1
          endif
       enddo
    end subroutine comp
    

    Then you can use the array indices as

    do i=1,size(indices)
       var = arr(indices(i))
    enddo
    

    Note that since you are not returning indices, you will lose all the information found once you return--unless you plan on using it in the subroutine, then you're okay. If you're not using it there, you could define it as a global variable in a module and the other subroutines should see it.

    0 讨论(0)
  • 2021-01-25 13:42

    If succinctness (rather than performance) floats your boat... consider:

    FUNCTION find_indexes_for_specific_elements_in_a_real_1D_array(array, min)  &
        RESULT(indices)
      REAL, INTENT(IN) :: array(:)
      REAL, INTENT(IN) :: min
      INTEGER, ALLOCATABLE :: indices(:)
      INTEGER :: i
      indices = PACK([(i,i=1,SIZE(array))], array >= min)
    END FUNCTION find_indexes_for_specific_elements_in_a_real_1D_array
    

    [Requires F2003. Procedures that have assumed shape arguments and functions with allocatable results need to have an explicit interface accessible where they are referenced, so all well behaved Fortran programmers put them in a module.]

    0 讨论(0)
提交回复
热议问题