在 Fortran 中从 p*p*n 数组中选择 k*k 子数组
我在 Fortran 中有一个 ppn 数组,我想从那个更大的数组中提取 k*k 子数组。我尝试这样做,但不确定它是否有效:
do i=1,p
vp(i)=i
end do
help=y(1:p,t)*vp
do t = 1, n
A(1:k,1:k,t) = B(pack(help,help>0), pack(help,help>0), t)
end do
其中 y 包含值 0 和 1,1 意味着行/列需要子数组。这可行吗?如果不行,如何实现同样的事情?谢谢。
I have a ppn array in Fortran, and I want to extract k*k subarray from that bigger array. I tried like this, but not sure does it work:
do i=1,p
vp(i)=i
end do
help=y(1:p,t)*vp
do t = 1, n
A(1:k,1:k,t) = B(pack(help,help>0), pack(help,help>0), t)
end do
where y contains values 0 and 1, 1 meaning that row/column is wanted to subarray. Does that work, and if not, how that same thing could be archieved? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果我明白你想要做什么,这里有一个示例程序,它提取选定的列和行,但不使用太多数组表示法。
If I understand what you want to do, here is an example program that extracts selected columns and rows, but not using much array notation.
解决方案二,在向量(一维数组)级别使用数组操作 - 将主循环替换为:
解决方案三,完全使用数组操作:
Solution two, using array operations at the vector (1D array) level -- replace the main loop with:
Solution three, fully using array operations:
不确定这些非代码片段对您是否有用,但是;
不要忘记,您可以使用向量下标来获取数组部分,例如,您可以像这样选择向量 v 的元素:
v( (/1, 3, 6, 5, 10 /) )
向量下标可以应用于秩大于 1 的数组。以这种方式弄清楚你的下标要求会让我很头疼,但你可能想要尝试一下。
Not sure if these fragments of non-code are any use to you but;
Don't forget that you can use vector subscripts to get array sections, for example, you might select elements of a vector v like this:
v( (/1, 3, 6, 5, 10 /) )
Vector subscripting can be applied to arrays of rank greater than 1. It would hurt my head to figure out your subscripting requirements this way, but you might want to try it.
是的,它应该可以工作,您甚至不需要使用
gfortran 4.2.3 正确编译的
do t = ...
循环给我您也可以使用
或考虑使用 LOGICAL .true。和.false。而不是 1 和 0...
Yes it should work, and you even do not need the
do t = ...
loopcompiled with gfortran 4.2.3 correctly gives me
You could also use
Or think using LOGICAL .true. and .false. instead of 1 and 0...