对数组进行按元素操作
我正在尝试通过访问 Fortran 中的越界元素来检查我的数组是否返回无意义的内容。我想检查这些值是否小于 1,如果小于,请将其更改为 1。
这是我的代码片段引起的问题:
lastNeighLabel=(/clusterLabel(jj-1,kk,ll), clusterLabel(jj,kk-1,ll), clusterLabel(jj,kk,ll-1)/ ) LastNeighLabel 分别包含 x、y、z 方向上最后一个邻居的簇标签(介于 1 和 n 之间,其中 n 是找到的唯一单独簇的总数)。
当jj
或kk
或ll
为1时,它们尝试访问数组中的第0个元素,并作为FORTRAN
从数组中的 1 开始计数,它试图摧毁宇宙。我目前正处于混乱的状态,大约有 8 个 if/elseif 语句,试图为每种可能性编写代码。但我希望有一种方法可以对每个元素进行操作。所以基本上我想说 where((/jj-1,kk-1,ll-1/).lt.1) do clusterLabel(jj-1,kk,ll)=0 等
取决于导致问题的元素。
但我想不出一种方法来做到这一点,因为 where 只会操作传递给它的变量,而不是同一索引处的不同数组。还是我错了?
如果这没有意义,我会很乐意编辑。
I'm trying to check if my arrays are returning nonsense by accessing out of bounds elements, in fortran. And I want to check these values are less than one, and if they are, change them to one.
This is the piece of my code causing issues:
lastNeighLabel=(/clusterLabel(jj-1,kk,ll), clusterLabel(jj,kk-1,ll), clusterLabel(jj,kk,ll-1)/)
LastNeighLabel contains the cluster label (between 1 and n, where n isthe total number of unique seperate clusters found) for the last neighbour in the x,y,z direction respectively.
When jj
or kk
or ll
are 1, they try and access the 0th element in the array, and as FORTRAN
counts from 1 in arrays, it tries to destroy the universe. I'm currently in a tangled mess of about 8 if/elseif statements trying to code for every eventuality. But I was hoping there was a way of operating on each element. So basically I'd like to say where((/jj-1,kk-1,ll-1/).lt.1) do clusterLabel(jj-1,kk,ll)=0 etc
depending on which element is causing the problem.
But I can't think of a way to do that because where will only manipulate the variables passed to it, not a different array at the same index. Or am I wrong?
Will gladly edit if this doesn't make sense.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Fortran 并不强制从 1 开始访问数组。允许任何起始值。如果使用零索引数组更方便,请将数组声明为:
或者
并额外添加 0 索引以捕获特殊情况。
这可能是解决您问题的另一种解决方案,因为零索引值是合法的。
It is not obligatory that Fortran accesses arrays starting from one. Any starting value is allowed. If it more convenient to you to have a zero indexed array, declare the array as:
Or
and have the 0 indices be extra to catch special cases.
This might be another solution to your problem, since zero index values would be legal.
另一种可能的解决方法是创建一个扩展的簇标签数组(索引范围从 0 开始),它等于在外部附加一层零的簇标签数组。然后,您可以让循环在 jj、kk 和 ll 的所有值上安全运行。这是否是可行的解决方案取决于数组的大小。
Another possible way to approach this, is to create an extended cluster label array (with index bounds starting at 0), which is equal to the cluster label array with a layer of zeroes tacked on the outside. You can then let your loop run safely over all values of jj, kk, and ll. It depends on the size of the array if this is a feasible solution.
也许你可以使用一个函数?
然后使用 f(clusterLabel,jj-1,kk,ll) 等。
Maybe you could use a function?
and then use f(clusterLabel,jj-1,kk,ll) etc.