多维数组长度不同 [英] Multidimensional array with different lengths

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

问题描述

我试图与在第二个层面例如不同长度的数组:

I am trying to make an array with different lengths in a second dimension e.g.:

  A = 1 3 5 6 9
      2 3 2
      2 5 8 9

这可能吗?我花的时间相当,但看不能找出任何一种方式。

Is this possible? I've spent a fair amount of time looking but cannot find out either way.

推荐答案

是,也不是。首先是没有:

Yes and no. First the no:

适当阵列Fortran语言,如声明如下:

Proper arrays in Fortran, such as those declared like this:

integer, dimension(3,3,4) :: an_array

或像这样

integer, dimension(:,:,:,:), allocatable :: an_array

是正规;对于每个维度,只有一个程度。

are regular; for each dimension there is only one extent.

但是,如果你想定义自己的类型,一个衣衫褴褛的阵列就可以了,这是比较容易的:

But, if you want to define your own type for a ragged array you can, and it's relatively easy:

type :: vector
    integer, dimension(:), allocatable :: elements
end type vector

type :: ragged_array
    type(vector), dimension(:), allocatable :: vectors
end type ragged_array

通过这种方法,您可以分配元素每个向量到不同大小的。例如:

With this sort of approach you can allocate the elements of each of the vectors to a different size. For example:

type(ragged_array) :: ragarr
...
allocate(ragarr%vectors(5))
...
allocate(ragarr%vectors(1)%elements(3))
allocate(ragarr%vectors(2)%elements(4))
allocate(ragarr%vectors(3)%elements(6))

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

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