Merging arrays in fortran 90/95

In summary, it seems that there is no intrinsic routine or function in Fortran 90/95 to merge 1d-arrays into a 2d-array, but it can be easily done through pointers or array sections.
  • #1
seneika
4
0
Hi,

can someone help me with this one?

I'm trying to merge several 1d-arrays into one 2d-array. Is there any intrinsic routine/function in fortran 90/95 to do so?

I also thought of pointers. Can I assign a 2d-pointer to more than one object so I have
a pointer array whose columns point to different 1d-target arrays?

I naively tried to do

...
double precision,dimension(:,:),pointer :: ptr_array
...

ptr_array(*,1)=>x(*)
ptr_array(*,2)=>y(*)
...

what didn't work. Can I do something like this?

Thanks!
 
Technology news on Phys.org
  • #2
seneika said:
Hi,

can someone help me with this one?

I'm trying to merge several 1d-arrays into one 2d-array. Is there any intrinsic routine/function in fortran 90/95 to do so?
As far as I know, there isn't, but it isn't that hard to do.
seneika said:
I also thought of pointers. Can I assign a 2d-pointer to more than one object so I have
a pointer array whose columns point to different 1d-target arrays?

I naively tried to do

...
double precision,dimension(:,:),pointer :: ptr_array
...

ptr_array(*,1)=>x(*)
ptr_array(*,2)=>y(*)
...

what didn't work. Can I do something like this?

Thanks!
I think something like this would work...
Fortran:
real :: numbersA(5) 
real :: numbersB(5)
real :: merged(2,5)
integer::i
numbersA = (/1.5, 3.2, 4.5, 0.9, 7.2 /)
numbersB = (/2.5, 2.2, 3.5,1.9, 6.2 /)
do i = 1, 5
   merged(1, i) = numbersA(i)
   merged (2, j) = numbersB(i)
end do
 
  • Like
Likes jedishrfu
  • #3
Alternatively, one can do it with array sections:

Fortran:
real :: A(2), B(2), C(2, 2)

A = (/ 1.0, 2.0 /)
B = (/ 3.0, 4.0 /)
C(1, :) = A
C(2, :) = B
 

1. How do I merge two arrays in Fortran 90/95?

To merge two arrays in Fortran 90/95, you can use the MERGE function. This function takes in two arrays of the same type and size, and returns a merged array with the elements from both arrays. Alternatively, you can also use a DO loop to iterate through both arrays and append the elements into a new array.

2. Can I merge arrays of different types in Fortran 90/95?

No, Fortran 90/95 does not allow you to merge arrays of different types. The arrays must have the same type and size in order to be merged using the MERGE function.

3. Is it possible to merge more than two arrays in Fortran 90/95?

Yes, it is possible to merge more than two arrays in Fortran 90/95. You can use the MERGE function multiple times, passing in the merged array from the previous merge as one of the input arrays.

4. How can I merge arrays in a specific order in Fortran 90/95?

You can specify the order of merging by simply changing the order of the input arrays in the MERGE function. The first array listed will be merged first, followed by the second array, and so on.

5. Are there any performance considerations when merging arrays in Fortran 90/95?

Yes, merging arrays can impact performance in Fortran 90/95. It is generally more efficient to merge arrays using a MERGE function rather than a DO loop, as the MERGE function is optimized for merging arrays. Additionally, the size and type of the arrays can also affect performance, so it is important to consider these factors when merging arrays in Fortran 90/95.

Similar threads

  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
20
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
19
Views
5K
Back
Top