【问题标题】:Realize an array which the second dimension depends on the first argument [duplicate]实现一个第二维取决于第一个参数的数组[重复]
【发布时间】:2021-08-14 05:36:54
【问题描述】:

假设我有一个数组A[a,b]a 的维度是2。当a=1时,b的维度为3;当a=2时,b的维度是6。

例如,一个数组A 看起来像

[[1,2,3]
[4,5,6,7,8,9]]

它由1*31*6 组合而成。总共 9 个条目,不是来自输入。

有什么方法可以在 Fortran 中定义这样的数组吗?还是我需要其他类型的数据结构?

【问题讨论】:

标签: arrays fortran


【解决方案1】:

您需要更好地描述您想要做什么。使用allocatable 实体,您可以轻松实现您所描述的。

program foo
  integer a, b
  real, allocatable :: x(:,:)
  read(*,*) a
  if (a == 1 .or. a == 2) then
     b = 3 * a
     allocate(x(a,b))
   else
     stop 'Is this right?'
  end if
  x = 1
  print *, shape(x)
end program foo

【讨论】:

  • 已编辑。很抱歉造成混乱。
猜你喜欢
  • 1970-01-01
  • 2018-03-22
  • 2013-01-10
  • 2016-07-27
  • 2019-02-09
  • 1970-01-01
  • 2022-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多