使用包含单个组件的派生类型可以实现特定情况的等效.单元阵列对应于该派生类型的数组,位于单元阵列的每个元素内的数组则是每个数组元素的数组分量.
就像是:
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...
Run Code Online (Sandbox Code Playgroud)
MATLAB中的单元比上面的特定类型具有更多的灵活性(这实际上更类似于结构的MATLAB概念).对于接近单元阵列灵活性的东西,您需要使用无限多态组件和其他中间类型定义.