【发布时间】:2014-07-21 18:28:13
【问题描述】:
我需要满足以下条件的单元格索引号:
Q(i)<=5 and V(i)/=1
(尺寸(Q)==尺寸(V))。我写了这样的东西:
program test
implicit none
integer, allocatable, dimension(:):: R
integer Q(5)
integer V(5)
integer counter,limit,i
counter=0
limit=5
V=(/0,0,1,0,0/)
Q=(/5,10,2,7,2/)
do i=1,5
if((Q(i)<=5).AND.(V(i)/=1)) then
counter=counter+1
end if
end do
allocate(R(counter))
counter=1
do i=1,5
if((Q(i)<=5).AND.(V(i)/=1)) then
R(counter)=i
counter=counter+1
end if
end do
deallocate(R)
end program test
但我不认为这是一个非常有效的 .有没有更好的办法解决这个问题?
我可以通过写删除一个循环
program test
implicit none
integer, allocatable, dimension(:):: R
integer Q(5)
integer V(5)
integer counter,limit,i
counter=0
limit=5
V=(/0,0,1,0,0/)
Q=(/5,10,2,7,2/)
V=-V+1
allocate(R((count(V*Q<=5)-count(V*Q==0))))
counter=1
do i=1,size(Q)
if((Q(i)<=5).AND.(V(i)==1)) then
R(counter)=i
counter=counter+1
end if
end do
end program test
【问题讨论】:
-
你的意思是像stackoverflow.com/q/21208593/3157076这样的吗?
-
是的,这和我的问题类似,但它也使用“可分配”。也许是“链表”?是不是更实用的解决方案?
-
一个可以使用链表,但这很可能会使用更多涉及的动态内存管理。对于像这样的简单情况,我倾向于根据“我编写正确代码需要多长时间”来衡量“效率”。在这种情况下,另一个问题的答案 - 将两个循环降低到一行 - 这样做。如果您有其他考虑,请在问题中详细说明。
-
你的意思是删除一个循环吗?我做了一些改变:程序测试隐式无整数,可分配,维度(:)::R整数Q(5)整数V(5)整数计数器,限制,i counter=0 limit=5 V=(/0,0,1,0,0/) Q=(/5,10,2,7,2/) V=-V+1 allocate(R((count (VQQ==0)))) counter=1 do i=1,size(Q) if((Q(i)
标签: arrays memory-management fortran fortran95