# Fortran - Manipulation Functions

#### Complete Python Prime Pack

9 Courses     2 eBooks

#### Artificial Intelligence & Machine Learning Prime Pack

6 Courses     1 eBooks

#### Java Prime Pack

9 Courses     2 eBooks

Manipulation functions are shift functions. The shift functions return the shape of an array unchanged, but move the elements.

Sr.No Function & Description
1

cshift(array, shift, dim)

It performs circular shift by shift positions to the left, if shift is positive and to the right if it is negative. If array is a vector the shift is being done in a natural way, if it is an array of a higher rank then the shift is in all sections along the dimension dim. If dim is missing it is considered to be 1, in other cases it has to be a scalar integer number between 1 and n (where n equals the rank of array ). The argument shift is a scalar integer or an integer array of rank n-1 and the same shape as the array, except along the dimension dim (which is removed because of the lower rank). Different sections can therefore be shifted in various directions and with various numbers of positions.

2

eoshift(array, shift, boundary, dim)

It is end-off shift. It performs shift to the left if shift is positive and to the right if it is negative. Instead of the elements shifted out new elements are taken from boundary. If array is a vector the shift is being done in a natural way, if it is an array of a higher rank, the shift on all sections is along the dimension dim. if dim is missing, it is considered to be 1, in other cases it has to have a scalar integer value between 1 and n (where n equals the rank of array). The argument shift is a scalar integer if array has rank 1, in the other case it can be a scalar integer or an integer array of rank n-1 and with the same shape as the array array except along the dimension dim (which is removed because of the lower rank).

3

transpose (matrix)

It transposes a matrix, which is an array of rank 2. It replaces the rows and columns in the matrix.

## Example

The following example demonstrates the concept −

```program arrayShift
implicit none

real, dimension(1:6) :: a = (/ 21.0, 22.0, 23.0, 24.0, 25.0, 26.0 /)
real, dimension(1:6) :: x, y
write(*,10) a

x = cshift ( a, shift = 2)
write(*,10) x

y = cshift (a, shift = -2)
write(*,10) y

x = eoshift ( a, shift = 2)
write(*,10) x

y = eoshift ( a, shift = -2)
write(*,10) y

10 format(1x,6f6.1)

end program arrayShift
```

When the above code is compiled and executed, it produces the following result −

```21.0  22.0  23.0  24.0  25.0  26.0
23.0  24.0  25.0  26.0  21.0  22.0
25.0  26.0  21.0  22.0  23.0  24.0
23.0  24.0  25.0  26.0   0.0   0.0
0.0    0.0  21.0  22.0  23.0  24.0
```

## Example

The following example demonstrates transpose of a matrix −

```program matrixTranspose
implicit none

interface
subroutine write_matrix(a)
integer, dimension(:,:) :: a
end subroutine write_matrix
end interface

integer, dimension(3,3) :: a, b
integer :: i, j

do i = 1, 3
do j = 1, 3
a(i, j) = i
end do
end do

print *, 'Matrix Transpose: A Matrix'

call write_matrix(a)
b = transpose(a)
print *, 'Transposed Matrix:'

call write_matrix(b)
end program matrixTranspose

subroutine write_matrix(a)

integer, dimension(:,:) :: a
write(*,*)

do i = lbound(a,1), ubound(a,1)
write(*,*) (a(i,j), j = lbound(a,2), ubound(a,2))
end do

end subroutine write_matrix
```

When the above code is compiled and executed, it produces the following result −

```Matrix Transpose: A Matrix

1  1  1
2  2  2
3  3  3
Transposed Matrix:

1  2  3
1  2  3
1  2  3
```
fortran_arrays.htm