Fortran等价于numpy.where()函数? [英] Fortran equivalent of numpy.where() function?
问题描述
我想在 Fortran 中做这样的事情:
program其中
real :: a(6)=(/ 4,5,6,7,8,9 /)
print *,a(a> 7)
结束程式
import numpy
a = numpy.array([4,5,6,7,8,9])
打印[numpy.where(a> 7)]
#或
打印a [a> 7]
我已经玩过,但到目前为止没有任何工作,但我猜这是相当简单的。
我会稍微扩展@VladimirF的答案,因为我怀疑你不想限制自己确切的打印示例
但是,您可以使用可能符合您的 a> 7
返回一个逻辑$ c $在符合条件的索引处使用
.true。
对应于 a
的数组 .false。
否则。 pack
内在函数使用这样一个掩码,并返回一个数组,其中包含 .true。
p>
numpy.where
愿望的掩码来执行其他操作。例如,其中
构造(和其中
语句)和 merge
pack
来获取索引并执行更多操作。
I would like to do something like this in Fortran:
program where
real :: a(6) = (/ 4, 5, 6, 7, 8, 9 /)
print *, a(a>7)
end program
In Python I would typically do this with NumPy like this:
import numpy
a = numpy.array([ 4, 5, 6, 7, 8, 9])
print a[numpy.where(a>7)]
#or
print a[a>7]
I've played around, but nothing has worked thus far, but I'm guessing it is fairly simple.
I'll extend slightly the answer by @VladimirF as I suspect you don't want to limit yourself to the exact print example.
a>7
returns a logical
array corresponding to a
with .true.
at index where the condition is met, .false.
otherwise. The pack
intrinsic takes such a mask and returns an array with those elements with .true.
in the mask.
However, you can do other things with the mask which may fit under your numpy.where
desire. For example, there is the where
construct (and where
statement) and the merge
intrinsic. Further you can use pack
again with the mask to get the indices and do more involved manipulations.
这篇关于Fortran等价于numpy.where()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!