如何检查 Fortran 数组是否包含值?
我见过这要求其他语言,但刚刚发现 Fortran 可以很好地处理数组,我认为可能有一种简单的方法可以在没有循环的情况下做到这一点。
目前,我正在搜索一个 3D 数组,查看“最近的邻居”,看看它们是否包含字母“n”,每当它找到这个值时,我希望它执行一些 clusterLabel 分配(这与这个问题无关) )
我想使用 if(lastNeighArray.eq."n") then...<其余代码>
但由于显而易见的原因,它不喜欢根据值检查数组。它也不喜欢我使用 lastNeighArray(:)
,尽管我希望它一次检查每个元素。 where(lastNeighArray.eq."n")
不起作用,因为我在 where 循环内有一个 case 语句,并且收到错误 WHERE 语句和构造不得嵌套。
代码>
所以我有点卡住了。我真正想要的是类似于 when(lastNeighArray.eq."n")
但那不存在。
我还查看了 any
和 forall
但它们似乎不是正确的选择。
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.
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)
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.
So I'm a little stuck. What I really want is something like when(lastNeighArray.eq."n")
but that doesn't exist.
I've also looked at any
and forall
but they don't seem like the right choice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ANY 实际上应该是正确的选择,
如果您希望整个数组包含该值,也可以选择 ALL。
ANY should actually be the right choice
there is also ALL if you wanted the whole array to contain that value.