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

concatenate arrays

Nigel_Thomas
Beginner
4,597 Views
whats the best way to concatenate one dimensional arrays?

real*8 omega(200).omega1(50),omega2(50),omega3(50),omega4(50)

I initially optimistically just tried

omega=omega1+omega2+omega3+omega4

not quite sure what that actually does, but not what i was wanting.

Ended up just with :-

omega(1:50) = omega1
omega(51:100) = omega2
omega(101:150) = omega3
omega(151:200) = omega4

but there must be a more elegant way - particularly if the array sizes are variables

thanks
0 Kudos
5 Replies
TimP
Honored Contributor III
4,597 Views
Quoting - Nigel Thomas
whats the best way to concatenate one dimensional arrays?

real*8 omega(200).omega1(50),omega2(50),omega3(50),omega4(50)

I initially optimistically just tried

omega=omega1+omega2+omega3+omega4

not quite sure what that actually does, but not what i was wanting.

Ended up just with :-

omega(1:50) = omega1
omega(51:100) = omega2
omega(101:150) = omega3
omega(151:200) = omega4

but there must be a more elegant way - particularly if the array sizes are variables

thanks
No, unless you construct one of your own, the only concatenation operator is // for character strings.

omega=omega1+omega2+omega3+omega4
should be diagnosed as an error, if all checks are enabled. The expression on the right adds the arrays element by element, but the size doesn't match the destination.

The array section assignments, for whose simplicity you appear to be apologizing, look just fine. You could easily extend to variable sizes with the SIZE operator, for example.

No doubt, you could define your own "elegant" operators, which might approach the "best" way in your opinion to make it difficult for others to understand.

As pointed out below,
(/ omega1, omega2, omega3, omega4 /) ! f90 notation
[ omega1, omega2, omega3, omega4 ] ! f2003 alternative

would accomplish it, possible with weaker performance.


0 Kudos
henning_woehler
Beginner
4,597 Views
Quoting - Nigel Thomas
whats the best way to concatenate one dimensional arrays?

real*8 omega(200).omega1(50),omega2(50),omega3(50),omega4(50)

I initially optimistically just tried

omega=omega1+omega2+omega3+omega4

not quite sure what that actually does, but not what i was wanting.

Ended up just with :-

omega(1:50) = omega1
omega(51:100) = omega2
omega(101:150) = omega3
omega(151:200) = omega4

but there must be a more elegant way - particularly if the array sizes are variables

thanks

0 Kudos
henning_woehler
Beginner
4,597 Views

what about

omega = [omega1,omega2,omega3,omega4] ?

Henning
0 Kudos
Nigel_Thomas
Beginner
4,597 Views
Quoting - henning_woehler

what about

omega = [omega1,omega2,omega3,omega4] ?

Henning
well, i never knew that syntax, when did that come in.

Works great, exactly what i was looking for. Similar to matlab syntax actually.
0 Kudos
Steven_L_Intel1
Employee
4,597 Views

The square bracket syntax for an array constructor is new in Fortran 2003, though Intel Fortran has supported it for a very long time. The use of an array constructor to build a new array out of other arrays is in Fortran 90.
0 Kudos
Reply