The SPREAD function adds a dimension to an array by adding copies of a data object along a given dimension.

## Syntax

SPREAD (source,dim,ncopies)

*source* is an INTENT(IN) scalar or array of any type. Its rank must be less than seven.

*dim* is an INTENT(IN) scalar of type INTEGER with a value in the range *x* ≤ *n**n* is the rank of source.

*ncopies* is an INTENT(IN) scalar of type INTEGER.

The result is an array of the same type and kind as *source* and of rank *n* + 1, where *n* is the rank of *source*.

If *source* is scalar, the shape of the result is MAX(*ncopies*, 0) and each element of the result has a value equal to *source*.

If *source* is an array with shape (d_{1}, d_{2}, ..., d_{n}), the shape of the result is (d_{1}, d_{2}, ..., d_{dim-1}, MAX(ncopies, 0), d_{dim+1}, ..., d_{n}) and the element of the result with subscripts (r_{1}, r_{2}, ..., r_{n+1}) has the value source(r_{1}, r_{2}, ..., r_{dim-1}, r_{dim+1}, ..., r_{n+1}).

## Example

integer :: b(2,2)=reshape((/1,2,3,4/),shape(b)) ! show how shape of array changes after spreading write(*,*) shape(b) ! writes 2 2 write(*,*) shape(spread(b,1,3)) ! writes 3 2 2 write(*,*) shape(spread(b,2,3)) ! writes 2 3 2 write(*,*) shape(spread(b,3,3)) ! writes 2 2 3 ! show element values after spreading write(*,*) b ! writes 1 2 3 4 write(*,*) spread(b,1,3) ! writes 1 1 1 2 2 2 3 3 3 4 4 4 write(*,*) spread(b,2,3) ! writes 1 2 1 2 1 2 3 4 3 4 3 4 write(*,*) spread(b,3,3) ! writes 1 2 3 4 1 2 3 4 1 2 3 4