merge(3f) - [FORTRAN:INTRINSIC:ARRAY CONSTRUCTION] Merge variables
result = merge(tsource, fsource, mask)
Select values from two arrays or scalars according to a logical mask. The result is equal to TSOURCE if MASK is .true., or equal to FSOURCE if it is .false. .
TSOURCE May be of any type. FSOURCE Shall be of the same type and type parameters as TSOURCE. MASK Shall be of type LOGICAL.
The result is of the same type and type parameters as TSOURCE. For any element the result is TSOURCE if MASK is true and FSOURCE otherwise.
The value of
merge (1.0, 0.0, k > 0)is 1.0 for K=5 and 0.0 for K=2.
Note that (currently) CHARACTER values must be of the same length.
program demo_merge implicit none integer :: tsrc(2,3), fsrc(2,3), answer(2,3) logical :: mask(2,3) integer :: i tsrc(1,:)=[ 1,6,5 ]; fsrc(1,:)=[ 0,3,2 ]; & & mask(1,:)=[.true., .false.,.true.] tsrc(2,:)=[ 2,4,6 ]; fsrc(2,:)=[ 7,4,8 ]; & & mask(2,:)=[.false.,.false.,.true.] answer=merge(tsrc,fsrc,mask) write(*,(3i2))(answer(i,:),i=1,size(answer,dim=1)) end program demo_mergeExpected result
> 1 3 5 > 7 4 6
[[Fortran 95]] and later
[[Elemental procedure|Elemental function]]
merge (3) | March 11, 2021 |