This doesn't work
program main
implicit none
integer :: nx = 3
integer :: ny = 5
integer :: nz = 8
real, allocatable, dimension(:,:,:) :: A
real, allocatable, dimension(:,:) :: B
allocate(A(nx,0:ny,nz) )
! ...do something with array A and at some point cope a slice of A to B:
B = A(:,:,1)
! in this case B is (1:nx, 1: ny+1)
end program main
The code above automatically allocates B
and copies A(:,:,1)
to B
. However it doesn't keep the lower/upper bound of 0/ny, instead B has its lower bound to 1 and upper bound to ny+1.
The only way I found to keep the lower/upper bound of A
2dn-dim is by explicitly allocate B
as:
allocate(B(nx, 0:ny))
B = A(:,:,1)
! in this case B is (1:nx, 0:ny)
Given that I have many more variables than in this simple example, is there a way to assign like B=A(:,:,1)
and also keep the bounds of A
without explicitly allocating B
?