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

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

问题描述



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

解决方案

使用包含单个组件的派生类型可以实现您的特定情况的等效项。单元格数组对应于该派生类型的数组,每个数组元素的数组元素都包含在单元格数组的每个元素中的数组。

类似于:

 类型单元格
! Allocatable组件(F2003)允许运行时变化
!在组件的形状。
REAL,ALLOCATABLE :: component(:, :)
END类型单元格

!为了这个例子,单元阵列相当于
!是固定长度。
TYPE(Cell):: x(10)

!将组件分配到所需的长度。 (
ALLOCATE(x(1)%component(20,2))
ALLOCATE(x(2)%component(25,2) )
...
!使用x ...

MATLAB中的单元格比上述特定类型这实际上更类似于MATLAB的结构概念)。对于接近单元格数组的灵活性的东西,您需要使用无限多态组件和其他中间类型定义。


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

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.

How can I do this?

解决方案

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.

Something like:

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...

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天全站免登陆