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

ID 767251
Date 3/22/2024
Public

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

Document Table of Contents

PACK Function

Transformational Intrinsic Function (Generic): Takes elements from an array and packs them into a rank-one array under the control of a mask.

result = PACK (array,mask[,vector])

array

(Input) Must be an array. It may be of any data type.

mask

(Input) Must be of type logical and conformable with array. It determines which elements are taken from array.

vector

(Input; optional) Must be a rank-one array with the same type and type parameters as array. Its size must be at least t, where t is the number of true elements in mask. If mask is a scalar with value true, vector must have at least as many elements as there are in array.

Elements in vector are used to fill out the result array if there are not enough elements selected by mask.

Results

The result is a rank-one array with the same type and kind parameters as array. If vector is present, the size of the result is that of vector. Otherwise, the size of the result is the number of true elements in mask, or the number of elements in array (if mask is a scalar with value true).

Elements in array are processed in array element order to form the result array. Element i of the result is the element of array that corresponds to the ith true element of mask. If vector is present and has more elements than there are true values in mask, any result elements that are empty (because they were not true according to mask) are set to the corresponding values in vector.

Example

N is the array

  [ 0  8  0 ]
  [ 0  0  0 ]
  [ 7  0  0 ].

PACK (N, MASK=N .NE. 0, VECTOR=(/1, 3, 5, 9, 11, 13/)) produces the result (7, 8, 5, 9, 11, 13).

PACK (N, MASK=N .NE. 0) produces the result (7, 8).

The following shows another example:

 INTEGER array(2, 3), vec1(2), vec2(5)
 LOGICAL mask (2, 3)
 array = RESHAPE((/7, 0, 0, -5, 0, 0/), (/2, 3/))
 mask = array .NE. 0
 !  array is  7  0 0  and mask is  T F F
 !            0 -5 0               F T F
 VEC1 = PACK(array, mask)      ! returns ( 7, -5 )
 VEC2 = PACK(array, array .GT. 0, VECTOR= (/1,2,3,4,5/))
 !  returns ( 7, 2, 3, 4, 5 )

See Also