Fortran Inner Array

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

Basically (for example) I'm trying to create an array X(10), where the element X(1)is an array with a size of (20,2), X(2)is an array with a size of (25,2), etc.

How can i do this?

+3
source share
1 answer

The equivalent for your particular case is achieved using a derived type that contains one component. An array of cells corresponds to an array of this derived type, the arrays that are inside each element of the array of cells are arrays of each element of the array.

Sort of:

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

+5

All Articles