Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
fortran查找数组中的整数序列_Fortran_Fortran90_Fortran95 - Fatal编程技术网

fortran查找数组中的整数序列

fortran查找数组中的整数序列,fortran,fortran90,fortran95,Fortran,Fortran90,Fortran95,Fortran中是否有一个函数或方法可以在数组中查找一系列整数,并返回数组中的位置或计数(如果匹配) 1,5,8,56,33,56,78,123,78,8,34,33,19,25,36 查找8,56,33 返回3作为位置或1作为匹配项 如果有多个: 1,5,8,56,33,56,78,123,78,8,56,33,19,25,36 查找8,56,33 返回3和10或2 fortran中有处理这种数组搜索的函数吗?简单的回答是没有,fortran中没有这样的函数 你通常被要求自己写这样的东西。例如

Fortran中是否有一个函数或方法可以在数组中查找一系列整数,并返回数组中的位置或计数(如果匹配)

1,5,8,56,33,56,78,123,78,8,34,33,19,25,36

查找8,56,33

返回3作为位置或1作为匹配项

如果有多个:

1,5,8,56,33,56,78,123,78,8,56,33,19,25,36

查找8,56,33

返回3和10或2


fortran中有处理这种数组搜索的函数吗?

简单的回答是没有,fortran中没有这样的函数

你通常被要求自己写这样的东西。例如:

从所有可能的起始索引数组开始 确定保持索引的条件 按顺序只保留那些满足条件的索引 内在过程在这里非常有用,它只能用于保留数组中的值,即与某个条件匹配的起始位置,即保留起始位置的条件

该产品未经广泛测试!下面的program test.f90说明了其用途:

module mod_finder
    implicit none

    contains
        subroutine find_start_locs(array, sub_array, start_locs)
            integer, intent(in) :: array(:)
            integer, intent(in) :: sub_array(:)
            integer, allocatable, intent(out) :: start_locs(:)
            integer :: i

            ! initialize result with all possible starting indices
            start_locs = [(i, i = 1, size(array)-size(sub_array)+1)]

            ! sequentially keep only those indices that satisfy a condition
            do i = 1, size(sub_array)
                ! condition for keeping: the value of array(start_locs + i - 1) must be equal to the value of sub_array(i)
                ! use PACK to only keep start_locs that satisfy this condition
                start_locs = PACK(start_locs, array(start_locs + i - 1) == sub_array(i))
                if (size(start_locs) == 0) then
                    exit
                end if
            end do
        end subroutine find_start_locs

end module mod_finder

program test
    use mod_finder
    implicit none

    integer, allocatable :: arr(:)
    integer, allocatable :: seq(:)
    integer, allocatable :: res(:)

    ! arr = [1, 5, 8, 56, 33, 56, 78, 123, 78, 8, 34, 33, 19, 25, 36]
    arr = [1, 5, 8, 56, 33, 56, 78, 123, 78, 8, 56, 33, 19, 25, 36]
    seq = [8, 56, 33]

    call find_start_locs(arr, seq, res)

    print *, "array:     ", arr
    print *, "sequence:  ", seq
    print *, "locations: ", res
    print *, "# matches: ", size(res)

end program test
对于问题中的两个测试用例,编译和运行会产生以下输出:

$ gfortran -O2 -g -Wall -Wextra -fcheck=all test.f90
$ ./a.out

 array:                1           5           8          56          33          56          78         123          78           8          34          33          19          25          36
 sequence:             8          56          33
 locations:            3
 # matches:            1


简而言之,答案是否定的,Fortran中没有这样的函数

你通常被要求自己写这样的东西。例如:

从所有可能的起始索引数组开始 确定保持索引的条件 按顺序只保留那些满足条件的索引 内在过程在这里非常有用,它只能用于保留数组中的值,即与某个条件匹配的起始位置,即保留起始位置的条件

该产品未经广泛测试!下面的program test.f90说明了其用途:

module mod_finder
    implicit none

    contains
        subroutine find_start_locs(array, sub_array, start_locs)
            integer, intent(in) :: array(:)
            integer, intent(in) :: sub_array(:)
            integer, allocatable, intent(out) :: start_locs(:)
            integer :: i

            ! initialize result with all possible starting indices
            start_locs = [(i, i = 1, size(array)-size(sub_array)+1)]

            ! sequentially keep only those indices that satisfy a condition
            do i = 1, size(sub_array)
                ! condition for keeping: the value of array(start_locs + i - 1) must be equal to the value of sub_array(i)
                ! use PACK to only keep start_locs that satisfy this condition
                start_locs = PACK(start_locs, array(start_locs + i - 1) == sub_array(i))
                if (size(start_locs) == 0) then
                    exit
                end if
            end do
        end subroutine find_start_locs

end module mod_finder

program test
    use mod_finder
    implicit none

    integer, allocatable :: arr(:)
    integer, allocatable :: seq(:)
    integer, allocatable :: res(:)

    ! arr = [1, 5, 8, 56, 33, 56, 78, 123, 78, 8, 34, 33, 19, 25, 36]
    arr = [1, 5, 8, 56, 33, 56, 78, 123, 78, 8, 56, 33, 19, 25, 36]
    seq = [8, 56, 33]

    call find_start_locs(arr, seq, res)

    print *, "array:     ", arr
    print *, "sequence:  ", seq
    print *, "locations: ", res
    print *, "# matches: ", size(res)

end program test
对于问题中的两个测试用例,编译和运行会产生以下输出:

$ gfortran -O2 -g -Wall -Wextra -fcheck=all test.f90
$ ./a.out

 array:                1           5           8          56          33          56          78         123          78           8          34          33          19          25          36
 sequence:             8          56          33
 locations:            3
 # matches:            1


你所追求的是一种叫做滑动窗口搜索算法的东西。一个简单的实现方法如下:

program test
  implicit none

  integer, dimension(:), allocatable :: arr 
  integer, dimension(:), allocatable :: seq
  integer                            :: i

  arr = [1, 5, 8, 56, 33, 56, 78, 123, 78, 8, 56, 33, 19, 25, 36]
  seq = [8, 56, 33]

  do i=1,size(arr)-size(seq)+1
     if (all(arr(i:i+size(seq)-1) == seq)) print *, i
  end do
end program

这不是最优化的版本,但它在大多数情况下都能完成任务。

您所追求的是一种称为滑动窗口搜索算法的算法。一个简单的实现方法如下:

program test
  implicit none

  integer, dimension(:), allocatable :: arr 
  integer, dimension(:), allocatable :: seq
  integer                            :: i

  arr = [1, 5, 8, 56, 33, 56, 78, 123, 78, 8, 56, 33, 19, 25, 36]
  seq = [8, 56, 33]

  do i=1,size(arr)-size(seq)+1
     if (all(arr(i:i+size(seq)-1) == seq)) print *, i
  end do
end program

这不是最优化的版本,但在大多数情况下它都能完成任务。

不完全是您想要的,但查看一下以完成我以前的评论,findloc已添加到,而不完全是您想要的,但是查看一下以完成我以前的评论,findloc已添加到,如果您正在使用大型阵列,您可能希望修改find_start_locs子例程,以使用中概述的方法来构建start_locs,而不是像我这样剥离。除了初始开销之外,我的pack中的条件掩码还需要访问数组的非连续元素,而滑动窗口访问的元素更为连续如果您使用的是大型数组,您可能希望修改find_start_locs子例程,以使用中概述的方法来构建start_locs,而不是像我这样剥离。除了初始开销之外,pack中的条件掩码还需要访问数组中的非连续元素,而滑动窗口访问元素的方式更为连续