Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference

ID 767251
Date 11/07/2023
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

MERGE

Elemental Intrinsic Function (Generic): Selects between two values or between corresponding elements in two arrays, according to the condition specified by a logical mask.

result = MERGE (tsource,fsource,mask)

tsource

(Input) May be of any data type.

fsource

(Input) Must be of the same type and type parameters as tsource.

mask

(Input) Must be of type logical.

Results

The result type and kind are the same as tsource. The value of mask determines whether the result value is taken from tsource (if mask is true) or fsource (if mask is false).

Example

For MERGE (1.0, 0.0, R < 0), R = -3 has the value 1.0, and R = 7 has the value 0.0.

TSOURCE is the array

  [ 1  3  5 ]
  [ 2  4  6 ],

FSOURCE is the array

  [ 8  9  0 ]
  [ 1  2  3 ],

and MASK is the array

  [ F  T  T]
  [ T  T  F].

MERGE (TSOURCE, FSOURCE, MASK) produces the result:

  [ 8  3  5 ]
  [ 2  4  3 ].

The following shows another example:

 INTEGER tsource(2, 3), fsource(2, 3), AR1 (2, 3)
 LOGICAL mask(2, 3)
 tsource = RESHAPE((/1, 4, 2, 5, 3, 6/),(/2, 3/))
 fsource = RESHAPE((/7, 0, 8, -1, 9, -2/), (/2, 3/))
 mask = RESHAPE((/.TRUE., .FALSE., .FALSE., .TRUE.,         &
                  .TRUE., .FALSE./), (/2,3/))
 ! tsource is  1 2 3 , fsource is  7  8  9 , mask is  T F T
 !             4 5 6               0 -1 -2            F T F

 AR1 = MERGE(tsource, fsource, mask) ! returns  1 8  3
                                     !          0 5 -2
 END