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

2023-04-15Python开发问题
4

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

问题描述

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

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

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

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.

推荐答案

我将稍微扩展@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.

这篇关于Fortran 相当于 numpy.where() 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

pandas 有从特定日期开始的按月分组的方式吗?
Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)...
2024-08-22 Python开发问题
10

如何对具有重复列名行进行切片,并按顺序堆叠这些行
How to slice a row with duplicate column names and stack that rows in order(如何对具有重复列名行进行切片,并按顺序堆叠这些行)...
2024-08-22 Python开发问题
6

通过在不与其他值冲突的情况下追加值来生成序列
Generate a sequence by appending values without clash in other values(通过在不与其他值冲突的情况下追加值来生成序列)...
2024-08-22 Python开发问题
7

使用MALLOC_MMAP_THRESHOLD_和MALLOC_MMAP_MAX_减少内存碎片
Reduce memory fragmentation with MALLOC_MMAP_THRESHOLD_ and MALLOC_MMAP_MAX_(使用MALLOC_MMAP_THRESHOLD_和MALLOC_MMAP_MAX_减少内存碎片)...
2024-08-22 Python开发问题
40

如何在Pandas中连接包含List(Series)的两列
How to concatenate two columns containing list (series) in Pandas(如何在Pandas中连接包含List(Series)的两列)...
2024-08-22 Python开发问题
2

我怎么才能让这个机器人连续运行呢?
How do I make this bot run continuously?(我怎么才能让这个机器人连续运行呢?)...
2024-08-22 Python开发问题
17