隐式 do 循环数组初始化
我想使用隐式 do 循环在一行上初始化一个数组。但是,我总是遇到语法或形状错误。谁能帮我纠正以下结构?
integer myarray :: (maxdim, nr)
myarray(1:maxdim,nr) = (/ (/i,i=1,maxdim/),nr /)
I want to initialize an array on one line with an implicit do loop. However, I always get a syntax or shape error. Can anyone help me correct the following construct?
integer myarray :: (maxdim, nr)
myarray(1:maxdim,nr) = (/ (/i,i=1,maxdim/),nr /)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在初始化一个包含
MAXDIM
行和NR
列的数组,并且每列看起来都包含整数 1 到MAXDIM
。第一步,继续写出实际的 DO 循环:
将内部循环折叠为隐式循环结构:
但是,当我们尝试折叠外部循环时,会发生一些奇怪的事情:
现在,我像你一样遇到了不兼容的排名错误。由于我也不太擅长隐式 do 循环,因此我查看了数组构造函数的
shape
内在结果
:任何嵌套数组结构。我们实际上可以删除第二组
(/ /)
来简化。由于一切都已经处于正确的顺序,我们可以使用reshape
内在函数来确保正确的排名。我的完整测试程序是:You are initializing an array with
MAXDIM
rows andNR
columns, and it looks like each column contains the integers 1 toMAXDIM
.As a first step, go ahead and write out the actual
DO
-loop:Collapse the inner loop to an implicit loop structure:
When we try to collapse the outer loop, though, something strange happens:
Now, I get an incompatible ranks error as you did. Since I'm not very good at the implicit do-loops either, I looked at the
shape
intrinsic results for the array constructor:This prints out
The array constructor is simply expanding a 1-D array , flattening any nested array constructions. We can actually drop the second set of
(/ /)
to simplify. Since everything is already in the proper order, we can use thereshape
intrinsic to ensure proper rank. My full test program is then:隐式 do 循环只会创建一个向量,因此您必须对其进行整形。像这样的东西:
或者也许您想要一个更复杂的、嵌套的、隐含的 do 循环:
请注意,我使用
[ ]
的 Fortran2003 约定来分隔数组结构,而不是(/ /)
。另请注意,您必须声明隐含的 do 循环索引变量。The implicit do loop will only create a vector so you'll have to reshape that. Something like this:
or perhaps you want a more complicated, nested, implied-do loop:
Note that I'm using the Fortran2003 convention of
[ ]
to delimit array constructions, rather than(/ /)
. Note also that you have to declare the implied do loop index variables.