numpy - Fortran 相当于 numpy.where() 函数?

标签 numpy fortran slice

我想在 Fortran 中做类似的事情:

program where

real :: a(6) = (/ 4, 5, 6, 7, 8, 9 /)

print *, a(a>7)

end program

Python我通常会使用 NumPy像这样:

import numpy

a = numpy.array([ 4, 5, 6, 7, 8, 9])

print a[numpy.where(a>7)]

#or 

print a[a>7]

我玩过,但到目前为止没有任何效果,但我猜它相当简单。

最佳答案

我将稍微扩展@VladimirF 的答案,因为我怀疑您不想将自己限制在确切的打印示例中。

a>7 返回与 a 对应的 logical 数组,其中 .true. 位于条件的索引处满足,.false. 否则。 pack 内部函数采用这样的掩码并返回一个数组,其中包含掩码中带有 .true. 的那些元素。

但是,您可以使用可能适合您 numpy.where 愿望的掩码做其他事情。例如,有 where 构造(和 where 语句)和 merge 内在函数。此外,您可以再次使用 pack 和掩码来获取索引并进行更多相关操作。

关于numpy - Fortran 相当于 numpy.where() 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21435589/

相关文章:

datetime - Fortran 中的日期时差

python - 取最近N天的平均值

python - 对一维 numpy 数组进行下采样

pointers - Fortran 中的故意类型不匹配

numpy - 是否有 Julia 相当于 NumPy 的省略号切片语法 (...)?

swift - 为什么 Slice<T> 的容量总是相等的?

string - Golang 在字符串 slice 中追加字符串

python - 什么是最好的 pythonic 解决方案?

python - ImportError : Missing required dependencies ['numpy' ]. 没有任何帮助

fortran - FORTRAN 90中同一行上的多个语句