CSHIFT(ARRAY, SHIFT, DIM)

Purpose

Shifts the elements of all vectors along a given dimension of an array. The shift is circular; that is, elements shifted off one end are inserted again at the other end.

Class

Transformational function

Argument type and attributes

ARRAY
An array of any type.
SHIFT
  • An INTENT(IN) INTEGER scalar, if ARRAY has a rank of one
  • An INTENT(IN) INTEGER scalar or an INTEGER expression of rank rank(ARRAY)-1, if ARRAY does not have a rank of one.
DIM (optional)
An INTENT(IN) INTEGER scalar. Its value must be in the range 1 ≤ DIM ≤ rank(ARRAY). If absent, it defaults to 1.

Result value

The result is an array with the same shape, data type, and type parameters as ARRAY.

If SHIFT is a scalar, the same shift is applied to each vector. Otherwise, each vector ARRAY (s1, s2, …, s(DIM-1), :, s(DIM+1), …, sn) is shifted according to the corresponding value in SHIFT (s1, s2, …, s(DIM-1), s(DIM+1), …, sn)

The absolute value of SHIFT determines the amount of shift. The sign of SHIFT determines the direction of the shift:
Positive SHIFT
moves each element of the vector toward the beginning of the vector.
Negative SHIFT
moves each element of the vector toward the end of the vector.
Zero SHIFT
does no shifting. The value of the vector remains unchanged.

Examples

! A is the array | A D G |
!                | B E H |
!                | C F I |

! Shift the first column down one, the second column
! up one, and leave the third column unchanged.
       RES = CSHIFT (A, SHIFT = (/-1,1,0/), DIM = 1)
! The result is | C E G |
!               | A F H |
!               | B D I |

! Do the same shifts as before, but on the rows
! instead of the columns.
       RES = CSHIFT (A, SHIFT = (/-1,1,0/), DIM = 2)
! The result is | G A D |
!               | E H B |
!               | C F I |