Fortran Wiki
unpack

Description

Store the elements of vector in an array of higher rank.

Standard

Fortran 95 and later

Class

Transformational function

Syntax

result = unpack(vector, mask, field)

Arguments

  • vector - Shall be an array of any type and rank one. It shall have at least as many elements as mask has true values.
  • mask - Shall be an array of type logical.
  • field - Shall be of the same type as vector and have the same shape as mask.

Return value

The resulting array corresponds to field with true elements of mask replaced by values from vector in array element order.

Example

program test_unpack
  integer :: vector(2)  = (/1,1/)
  logical :: mask(4)  = (/ .true., .false., .false., .true. /)
  integer :: field(2,2) = 0, unity(2,2)

  ! result: unity matrix
  unity = unpack(vector, reshape(mask, (/2,2/)), field)
end program

See also

pack, spread

category: intrinsics