数组内的 Fortran 数组 [英] Fortran array inside array

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

问题描述

我正在尝试在 Fortran 中创建一个类似于 MATLAB 中的单元格的数组.

I am trying to create an array in Fortran similar to a cell in MATLAB.

基本上(例如)我正在尝试创建一个数组 X(10) 其中元素 X(1) 是一个维度为 (20,2) 的数组, X(2) 是一个维度为 (25,2) 等的数组

Basically (for example) I am trying to create an array X(10) where the element X(1) is an array with dimension (20,2), X(2) is an array with dimension (25,2), etc.

我该怎么做?

推荐答案

使用包含单个组件的派生类型来实现特定情况的等价物.元胞数组对应于该派生类型的数组,位于元胞数组每个元素内的数组就是每个数组元素的数组组件.

The equivalent for your specific case is achieved using a derived type, that contains a single component. The cell array corresponds to an array of that derived type, the arrays that sit inside each element of the cell array are then the array components of each array element.

类似:

TYPE Cell
  ! Allocatable component (F2003) allows runtime variation 
  ! in the shape of the component.
  REAL, ALLOCATABLE :: component(:,:)
END TYPE Cell

! For the sake of this example, the "cell array" equivalent 
! is fixed length.
TYPE(Cell) :: x(10)

! Allocate components to the required length.  (Alternative 
! ways of achieving this allocation exist.)
ALLOCATE(x(1)%component(20,2))
ALLOCATE(x(2)%component(25,2))
...
! Work with x...

MATLAB 中的单元格比上述特定类型具有更大的灵活性(这实际上更类似于 MATLAB 的结构体概念).对于接近元胞数组灵活性的东西,您需要使用无限的多态组件和进一步的中间类型定义.

Cells in MATLAB have much more flexibility than given by the specific type above (this is really more akin to the MATLAB concept of a structure). For something that approaches the flexibility of a cell array you would need to use an unlimited polymorphic component and further intermediate type definitions.

这篇关于数组内的 Fortran 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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