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

编程入门 行业动态 更新时间:2024-10-26 05:31:21
问题描述

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

I would like to do something like this in Fortran:

program wherereal :: a(6) = (/ 4, 5, 6, 7, 8, 9 /)print *, a(a>7)end program

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

In Python I would typically do this with NumPy like this:

import numpya = 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.

推荐答案

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

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

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.

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

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.

  • 0
  • 0
  • 0
  • 0
  • 0

更多推荐

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

本文发布于:2023-05-22 18:54:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1326142.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!