Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reshape 1

Status
Not open for further replies.

Eide

Technical User
Apr 22, 2010
7
NO
Hi,

I need to reshape a one dimensional array into three three-dimensional arrays and I wonder how to do this correctly. In the code sample below you can see how the big one dimensional array is formed from the three three-dimensional ones. But now I need to do the opposite operation. Would be nice if someone could give me a hint. Thanks in advance

Eide

..... code sample ....

!
! variables
!
real (kind = 4), dimension:),:,:) :: vector_x, vector_y, vector_z
real (kind = 4), dimension:)) :: vector_full
integer (kind = 4) :: nx,ny,nz
integer (kind = 4) :: ndfx,ndfy,ndfz,ndf_full

!
! init
!
allocate(vector_x(nx-1,ny,nz))
allocate(vector_y(nx,ny-1,nz))
allocate(vector_z(nx,ny,nz-1))
!
! degrees of freedom
!
ndfx = (nx-1) * ny * nz
ndfy = nx * (ny-1) * nz
ndfz = nx * ny * (nz-1)

!
! some computation ...
!

!
! memory for big array
!
ndf_full = ndfx + ndfy + ndfz
allocate(full_vector(ndf_full))
!
! map the three vectors into a single one dimensional array
!
full_vector( 1:ndfx ) = reshape(vector_x,(/ndfx/))
full_vector( ndfx+1:ndfx+ndfy ) = reshape(vector_y,(/ndfy/))
full_vector(ndfy+ndfx+1:ndfx+ndfy+ndfz) = reshape(vector_z,(/ndfz/))
!
! some computation ...
!

!
! map back into the three three-dimensional arrays
!
! ????
 
What about

Code:
new_vector_x = reshape (fullvector (1 : ndfx), (/nx-1, ny, nz/)) 
new_vector_y = reshape (fullvector (ndfx + 1 : ndfx + ndfy),(/nx, ny - 1, nz/))
new_vector_z = reshape (fullvector (ndfx + ndfy + 1 : ndfx + ndfy + ndfz), (/nx, ny, nz-1/))

(but I must confess I did not use reshape for this purpose yet...)
Norbert

The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
You are right, I checked and it's correct.
Sometimes it's easier as it seems : )
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top