|
我今天写了一个关于矩阵展开的函数,作用是将四维数组沿每个维度展成二维矩阵,调用展开函数的时候发现了问题,我声明的指针数组在指向子函数中生成的二维矩阵时,数组每行最后两个值丢失,变成了极小数。是我指针传递过程中地址出现了问题嘛,请各位老师指教。附代码如下:program main implicit none
!声明接口
interface
subroutine PMF_unfold(A,ncmpx,ncmpy,nhx,nhy,f,B)
integer::icmpx,icmpy,ihx,ihy,m,n,f
integer::ncmpx,ncmpy,nhx,nhy
real*4::A(ncmpx,ncmpy,nhx,nhy)
real*4,pointer::B(:,:)
real*4,target::B1(ncmpx,ncmpy*nhx*nhy),B2(ncmpy,ncmpx*nhx*nhy),B3(nhx,ncmpx*ncmpy*nhy),B4(nhy,ncmpx*ncmpy*nhx)
end subroutine
end interface
integer::icmpx,icmpy,ihx,ihy,f
real*4,allocatable::A1(:,:,:,:),A2(:,:,:,:)
real*4,allocatable,target::B1(:,:),B2(:,:),B3(:,:),B4(:,:)
real*4,pointer::B(:,:)
integer::ncmpx,ncmpy,nhx,nhy
ncmpx=3
ncmpy=4
nhx=5
nhy=6
allocate(A1(ncmpx,ncmpy,nhx,nhy))
allocate(A2(ncmpx,ncmpy,nhx,nhy))
allocate(B1(ncmpx,ncmpy*nhx*nhy),B2(ncmpy,ncmpx*nhx*nhy),B3(nhx,ncmpx*ncmpy*nhy),B4(nhy,ncmpx*ncmpy*nhx))
!生成四维数组
do ihy=1,nhy
do ihx=1,nhx
do icmpy=1,ncmpy
do icmpx=1,ncmpx
A1(icmpx,icmpy,ihx,ihy)=icmpx+(icmpy-1)*ncmpx+(ihx-1)*ncmpx*ncmpy+(ihy-1)*nhx*ncmpx*ncmpy+1000
end do
end do
end do
end do
f=4
!调用展开函数
call PMF_unfold(A1,ncmpx,ncmpy,nhx,nhy,f,B)
write(*,*)B(3,:)输出指针B
deallocate(B1,B2,B3,B4)
deallocate(A1,A2)
pause
end program
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
subroutine PMF_unfold(A,ncmpx,ncmpy,nhx,nhy,f,B)
implicit none
integer::icmpx,icmpy,ihx,ihy,m,n,f
integer::ncmpx,ncmpy,nhx,nhy
real*4::A(ncmpx,ncmpy,nhx,nhy)
real*4,pointer::B(:,:)
real*4,target::B1(ncmpx,ncmpy*nhx*nhy),B2(ncmpy,ncmpx*nhx*nhy),B3(nhx,ncmpx*ncmpy*nhy),B4(nhy,ncmpx*ncmpy*nhx)
if(f==4)then!当F等于四时
do ihy=1,nhy
do ihx=1,nhx
do icmpy=1,ncmpy
do icmpx=1,ncmpx
m=ihy
n=(ihx-1)*ncmpy*ncmpx+(icmpy-1)*ncmpx+icmpx
B4(m,n)=A(icmpx,icmpy,ihx,ihy)
end do
end do
end do
end do
B=>B4(:,:)
end if
end subroutine
|
|