如何检查 Fortran 数组是否包含值?

发布于 2024-12-18 17:46:29 字数 578 浏览 3 评论 0原文

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

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

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

所以我有点卡住了。我真正想要的是类似于 when(lastNeighArray.eq."n") 但那不存在。

我还查看了 anyforall 但它们似乎不是正确的选择。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灼疼热情 2024-12-25 17:46:29

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

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

如果您希望整个数组包含该值,也可以选择 ALL。

ANY should actually be the right choice

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文