Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

construct arrays from pointer

mariospapa
Beginner
1,065 Views
Suppose that you have an array A(2,6,10). Is it possible to construct a second array B(2,6) from pointers p1, p2?

p1 => A(1,1:6)
p2 => A(2,1:6)
0 Kudos
7 Replies
mecej4
Honored Contributor III
1,065 Views
There are many ways in which this can be done. Here is one that you can try to find out if it meets your needs.

I have ignored the ",10" part of your description of array A, just as your assignments to p1 and p2 did.

[fortran]program tptr

integer, dimension(2,6),target :: A, B
integer, pointer, dimension(:) :: p1,p2

data A/11,21,12,22,13,23,14,24,15,25,16,26/

p1 => A(1,:)     ! p1 and p2 are 1-D arrays
p2 => A(2,:)
B = reshape((/ p1, p2 /),(/2,6/))  !assemble the pieces into B

write(*,'(1x,6I4)')B

end program tptr

[/fortran]
0 Kudos
mariospapa
Beginner
1,065 Views
Thanks mecej!

Is it possible to do the same by avoiding the decleration of array B?
0 Kudos
mecej4
Honored Contributor III
1,065 Views
I don't understand what "do the same" is intended to mean, but the following modification (with references to array B removed) runs fine:

[fortran]program tptr

integer, dimension(2,6),target :: A
integer, pointer, dimension(:) :: p1,p2

data A/11,21,12,22,13,23,14,24,15,25,16,26/

p1 => A(1,:)     ! p1 and p2 are 1-D arrays
p2 => A(2,:)

write(*,'(1x,6I4)')reshape((/ p1, p2 /),(/2,6/))

end program tptr

[/fortran]
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,065 Views
Be mindful that p1 and p2 in the above example are stride 2 arrays that are 1D (p1 points to every other element of A and p2 points to the other every other elements of A).

Jim
0 Kudos
mariospapa
Beginner
1,065 Views
The problem is very simple:
Suppose the matrix A(2,3,:) which third dimension varies... also:

[ a11 a12 a13 ]
[ a21 a22 a23 ]

How can i extract the first two dimensions of matrix A and put them like this:

[ a11 a21 ]
[ a12 a22 ]
[ a13 a23 ]

using the 1D pointers p1, p2 which:

[p1] = [ a11 a12 a13 ]
[p2] = [ a21 a22 a23 ]
0 Kudos
mecej4
Honored Contributor III
1,065 Views
If that's what you want, no need to do chopping up and reassembling or using pointers.

Use TRANSPOSE !
0 Kudos
mariospapa
Beginner
1,065 Views
It's not working for non-square matrices!!!!!! I can't transpose a 3 dimension matrix!
0 Kudos
Reply