RESHAPE

Transformational Intrinsic Function (Generic): Constructs an array with a different shape from the argument array.

Syntax

result = RESHAPE (source, shape [, pad] [, order])

source
(Input) Must be an array (of any data type). It supplies the elements for the result array. Its size must be greater than or equal to PRODUCT(shape) if pad is omitted or has size zero.

shape
(Input) Must be an integer array of up to 7 elements, with rank one and constant size. It defines the shape of the result array. Its size must be positive; its elements must not have negative values.

pad
(Input; optional) Must be an array with the same type and kind parameters as source. It is used to fill in extra values if the result array is larger than source.

order
(Input; optional) Must be an integer array with the same shape as shape. Its elements must be a permutation of (1,2,...,n), where n is the size of shape. If order is omitted, it is assumed to be (1,2,...,n).

Results

The result is an array of shape shape with the same type and kind parameters as source. The size of the result is the product of the values of the elements of shape.

In the result array, the array elements of source are placed in the order of dimensions specified by order. If order is omitted, the array elements are placed in normal array element order.

The array elements of source are followed (if necessary) by the array elements of pad in array element order. If necessary, additional copies of pad follow until all the elements of the result array have values.

Note

In standard Fortran array element order, the first dimension varies fastest. For example, element order in a two-dimensional array would be (1,1), (2,1), (3,1) and so on. In a three-dimensional array, each dimension having two elements, the array element order would be (1,1,1), (2, 1, 1), (1, 2, 1), (2, 2, 1), (1, 1, 2), (2, 1, 2), (1, 2, 2), (2, 2, 2).

RESHAPE can be used to reorder a Fortran array to match C array ordering before the array is passed from a Fortran to a C procedure.

See Also

PACK, SHAPE, TRANSPOSE, Array Assignment Statements

Examples

RESHAPE ((/3, 4, 5, 6, 7, 8/), (/2, 3/)) has the value

  [ 3  5  7 ]
  [ 4  6  8 ].

RESHAPE ((/3, 4, 5, 6, 7, 8/), (/2, 4/), (/1, 1/), (/2, 1/)) has the value

  [ 3  4  5  6 ]
  [ 7  8  1  1 ].

The following shows another example:

 INTEGER AR1( 2, 5)
 REAL F(5,3,8)
 REAL C(8,3,5)
 AR1 = RESHAPE((/1,2,3,4,5,6/),(/2,5/),(/0,0/),(/2,1/))
 ! returns      1 2 3 4 5
 !              6 0 0 0 0
 !
 ! Change Fortran array order to C array order
 C = RESHAPE(F, (/8,3,5/), ORDER = (/3, 2, 1/))
 END