here is an example that illustrates basic use of reshape.
program ch0808
implicit none
integer, dimension (1:2,1:4) :: x
integer, dimension (1:8) :: y = (/ 1, 2, 3, 4, 5, 6, 7, 8 /)
integer, dimension (1:6) :: z = (/ 1, 2, 3, 4, 5, 6 /)
integer :: r, c
print *, ' Source array y'
print *, y
print *, ' Source array z'
print *, z
print *, ' Simple reshape sizes match'
x = reshape(y,(/2,4/))
do r = 1, 2
print *, (x(r,c),c=1,4)
end do
print *, ' Source 2 elements smaller pad with 0'
x = reshape(z,(/2,4/),(/0,0/))
do r = 1, 2
print *, (x(r,c),c=1,4)
end do
print *, ' As previous now specify order as 1*2'
x = reshape(z,(/2,4/),(/0,0/),(/1,2/))
do r = 1, 2
print *, (x(r,c),c=1,4)
end do
print *, ' As previous now specify order as 2*1'
x = reshape(z,(/2,4/),(/0,0/),(/2,1/))
do r = 1, 2
print *, (x(r,c),c=1,4)
end do
end program ch0808
the source is available at
http://www.fortranplus.co.uk/resources/ch0808_2.f90
it's taken from our latest book.
here is some text from the standard about reshape
reshape(source,shape,pad,order)
order must have the same shape as Shape and its value must be a permutation
of (1,2,...,n) where n is the size of Shape.
If Absent it is if it were present with the value (1,2,...,n)
I think you can see why Dick forgets the way the arguments go :-)
At this point in our courses most students start banging their
heads on the keyboard with the above explanation.
Cheers
Ian Chivers
Jane Sleightholme
-----Original Message-----
From: Fortran 90 List [mailto:[log in to unmask]] On Behalf Of
Dick Hendrickson
Sent: 02 September 2012 17:32
To: [log in to unmask]
Subject: Re: preserve shape while concatenating arrays
I'd try a RESHAPE of [b1, b2, b3,...] to NxM. I always forget which way the
arguments go, sorry.
The [ ... ] syntax is the same thing as the (/ ... /) syntax.
Basically, in the late 70s and early 80s not all keypunches supported [ or
].
Dick Hendrickson
On Sun, Sep 2, 2012 at 10:13 AM, Stefan Mauerberger
<[log in to unmask]> wrote:
> Hi There!
>
> Again, I have to bother you guys with some elementary difficulties
> constructing arrays. The issue is to bring existing arrays into a new,
> bigger one.
>
> Let assume a function foo( A ) which expects an array A of shape N x M .
> In addition, there are already some Arrays b1, b2, ... , bM of shape N .
> The task now is obvious. It is, to combine b1, ... , bM and pass it
> through function foo() .
>
> My first attempt was foo( (/ b1, b2, ... , bM /) ) . This
> unfortunately does not work because the shape gets lost.
> shape( (/ b1, b2, ... , bM /) ) = ( N*M, ) It becomes an array of rank
> 1 and size N*M. However, I want to have a shape of N x M !
>
> A solution I found - but which I do not like - is to use an auxiliary
> array. First allocate an array B of shape N x M and fill it as B(:,1)
> = b1, ... , B(:,M) = bM . As a third step B could be passed through
> foo( B
> ) . From my point of view, doing so for a several million times is
> just a waste of resources.
>
> Isn't there any nicer solution?
>
> Best,
> Stefan
>
> Btw.: What is the difference between (/ 77, 90, ... /) and [ 2003,
> 2008, ... ]
|