如何在 Fortran 中即时增加数组大小? [英] How to increase array size on-the-fly in Fortran?

查看:64
本文介绍了如何在 Fortran 中即时增加数组大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序通过 3D 数组运行,标记它找到的集群",然后进行一些检查以查看是否有任何相邻集群的标签高于当前集群.还有第二个数组保存正确的"集群标签.如果发现第 n 个相邻簇被正确标记,则将该元素分配给 0,否则将其分配给正确的标签(例如,如果第 n 个站点的标签为 2,而邻居的标签为 3,则该元素的第 3 个元素labelArray 设置为 2).老实说,我有充分的理由这样做!

My program is running though 3D array, labelling 'clusters' that it finds and then doing some checks to see if any neighbouring clusters have a label higher than the current cluster. There's a second array that holds the 'proper' cluster label. If it finds that the nth adjoining cluster is labelled correctly, that element is assigned to 0, otherwise is assigns it to the correct label (for instance if the nth site has label 2, and a neighbour is labeled 3, the 3rd element of the labelArray is set to 2). I've got a good reason to do this, honest!

我想要的是能够动态分配 labelArray 的第 n 个元素.我看过可分配的数组并将事物声明为 labelArray(*) 但我并不真正理解这些,尽管搜索了网络和 StackOverflow.

All I want is to be able to assign the nth element of the labelArray on the fly. I've looked at allocatable arrays and declaring things as labelArray(*) but I don't really understand these, despite searching the web, and StackOverflow.

因此,任何有关此操作的帮助都会很棒.

So any help on doing this would be awesome.

推荐答案

这是一个堆栈溢出问题,其中包含一些代码示例,展示了几种使用 Fortran 可分配数组的方法:如何获得先前未知的数组作为 Fortran 中函数的输出:声明、分配、测试是否已经被分配,使用新的 move_alloc 和分配分配.未显示有显式释放,因为示例在退出过程时使用 move_alloc 和自动释放.

Here is a Stack Overflow question with some code examples showing several ways of using Fortran allocatable arrays: How to get priorly-unkown array as the output of a function in Fortran: declaring, allocating, testing for being already being allocated, using the new move_alloc and allocation on assignment. Not shown there is explicit deallocation, since the examples are using move_alloc and automatic deallocation on exit of a procedure.

附言如果你想重复添加一个元素,你应该考虑你的数据结构方法.通过增长数组一次添加一个元素并不是一种有效的方法.在 Fortran 中将数组从 N 个元素增长到 N+1 可能意味着创建一个新数组并复制所有现有元素.更合适的数据结构可能是链表.您可以通过创建用户定义的类型和使用指针在 Fortran 中创建链表.您将成员链接在一起,从一个指向下一个.添加另一个成员的开销很小.缺点是按顺序访问列表的成员是最容易的.您没有使用索引以任何顺序访问成员的数组的简单能力.

P.S. If you want to repeatedly add one element you should think about your data structure approach. Adding one element at a time by growing an array is not an efficient approach. To grow an array from N elements to N+1 in Fortran will likely mean creating a new array and copying all of the existing elements. A more appropriate data structure might be a linked list. You can create a linked list in Fortran by creating a user-defined type and using pointers. You chain the members together, pointing from one to the next. The overhead to adding another member is minor. The drawback is that it is easiest to access the members of the list in order. You don't have the easy ability of an array, using indices, to access the members in any order.

我在网上找到的有关 Fortran 中链表的信息:http://www-uxsup.csx.cam.ac.uk/courses/Fortran/paper_12.pdfhttp://www.iag.uni-stuttgart.de/IAG/institut/abteilungen/numerik/images/4/4c/Pointer_Introduction.pdf

Info about linked lists in Fortran that I found on the web: http://www-uxsup.csx.cam.ac.uk/courses/Fortran/paper_12.pdf and http://www.iag.uni-stuttgart.de/IAG/institut/abteilungen/numerik/images/4/4c/Pointer_Introduction.pdf

这篇关于如何在 Fortran 中即时增加数组大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆