本文介绍了如何检查 Fortran 数组是否包含值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到这要求使用其他语言,但刚刚发现 Fortran 可以很好地处理数组,我认为可能有一种简单的方法可以在没有循环的情况下做到这一点.

I've seen this asked for other languages, but having just found out how nicely Fortran can handle arrays, I thought there might be an easy way to do this without loops.

目前我正在搜索一个 3D 数组,查看最近的邻居"以查看它们是否包含字母n",并且每当它找到这个值时,我希望它执行一些 clusterLabel 分配(这不相关对于这个问题)

Currently I'm searching over a 3D array looking at 'nearest neighbours' to see if they contain the letter 'n', and whenever it finds this value, I want it to perform some clusterLabel assignment (which isn't relevant for this question)

我想使用 if(lastNeighArray.eq."n") then...但出于显而易见的原因,它不喜欢根据值检查数组.它也不喜欢我使用 lastNeighArray(:),即使我希望它一次检查每个元素.where(lastNeighArray.eq."n") 不起作用,因为我在 where 循环中有一个 case 语句,并且出现错误 WHERE 语句和构造不能嵌套.代码>

I wanted to use if(lastNeighArray.eq."n") then...<rest of code>but for obvious reasons it doesn't like checking an array against a value. Neither does it like me using lastNeighArray(:), even though I'd like it to check each of the elements one at a time. where(lastNeighArray.eq."n") doesn't work as I have a case statement inside the where loop and I get the error WHERE statements and constructs must not be nested.

所以我有点卡住了.我真正想要的是 when(lastNeighArray.eq."n") 之类的东西,但它不存在.

So I'm a little stuck. What I really want is something like when(lastNeighArray.eq."n") but that doesn't exist.

我也看过 anyforall 但它们似乎不是正确的选择.

I've also looked at any and forall but they don't seem like the right choice.

推荐答案

ANY 实际上应该是正确的选择

ANY should actually be the right choice

if ( ANY( lastNeighArray=="n" ) ) then

如果您希望整个数组包含该值,也可以使用 ALL.

there is also ALL if you wanted the whole array to contain that value.

这篇关于如何检查 Fortran 数组是否包含值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 01:42