Fortran等价于numpy.where()函数? [英] Fortran equivalent of numpy.where() function?

查看:191
本文介绍了Fortran等价于numpy.where()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Fortran 中做这样的事情:

  program其中

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

print *,a(a> 7)

结束程式

Python 中,我通常会使用 NumPy 是这样的:

  import numpy 

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

打印[numpy.where(a> 7)]

#或

打印a [a> 7]

我已经玩过,但到目前为止没有任何工作,但我猜这是相当简单的。

解决方案

我会稍微扩展@VladimirF的答案,因为我怀疑你不想限制自己确切的打印示例

a> 7 返回一个逻辑 .true。对应于 a 的数组 .false。否则。 pack 内在函数使用这样一个掩码,并返回一个数组,其中包含 .true。 p>

但是,您可以使用可能符合您的 numpy.where 愿望的掩码来执行其他操作。例如,其中构造(和其中语句)和 merge code>内在。此外,您可以再次使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆