TExcelTAD
Home › Fortran › Fortran Multidimensional Arrays

Fortran Multidimensional Arrays

Fortran arrays can have up to 15 dimensions. Two-dimensional arrays (matrices) and three-dimensional arrays (fields on a grid) are everyday tools in scientific code.

Declaring and indexing

List the extent of each dimension, separated by commas. An element is addressed with one index per dimension: a(row, column).

fortran · try it yourself
program matrix_basics
  implicit none
  integer :: m(3, 4)          ! 3 rows, 4 columns
  integer :: i, j

  do j = 1, 4
    do i = 1, 3
      m(i, j) = 10 * i + j
    end do
  end do

  do i = 1, 3
    print '(4i4)', m(i, :)    ! row i: all columns
  end do
  print '(a, i0)', 'm(2, 3) = ', m(2, 3)
  print '(a, 2i3)', 'shape:   ', shape(m)
  print '(a, i0)', 'size:    ', size(m)
  print '(a, i0)', 'columns: ', size(m, dim=2)
end program matrix_basics
  11  12  13  14
  21  22  23  24
  31  32  33  34
m(2, 3) = 23
shape:     3  4
size:    12
columns: 4
Term Meaning For m(3, 4)
rank number of dimensions 2
extent number of elements along one dimension 3 and 4
shape the list of extents [3, 4]
size total number of elements 12

Column-major order

This is the single most important fact about Fortran arrays: elements are stored column by column ("column-major"). The first index varies fastest in memory. C, C++, Python/NumPy (by default) do the opposite.

fortran · try it yourself
program column_major
  implicit none
  integer :: m(3, 4)
  integer :: i, j
  do j = 1, 4
    do i = 1, 3
      m(i, j) = 10 * i + j
    end do
  end do
  ! printing the whole array lists elements in memory order
  print '(12i3)', m
end program column_major
 11 21 31 12 22 32 13 23 33 14 24 34

Printing the whole matrix gives the first column (11, 21, 31), then the second, and so on. That's why the row-by-row display above used m(i, :).

Performance rule: in nested loops, make the first index the innermost loop, as in the examples above. Then the program walks through memory contiguously, which uses the CPU cache efficiently. On large arrays the wrong loop order can be several times slower.

fortran
! good: inner loop over the first index
do j = 1, ncols
  do i = 1, nrows
    a(i, j) = a(i, j) * 2.0
  end do
end do

Sections of 2-D arrays

Slicing works in every dimension at once:

fortran · try it yourself
program sections_2d
  implicit none
  integer :: grid(4, 5)
  integer :: i, j

  grid = reshape([(i, i = 1, 20)], [4, 5])
  call show('grid', grid)
  call show('rows 2-3, cols 2-4', grid(2:3, 2:4))
  call show('every other column', grid(:, ::2))
  print '(a, *(i4))', 'column 5 as a vector:', grid(:, 5)

  grid(1, :) = 0         ! clear the first row
  call show('first row cleared', grid)

contains

  subroutine show(title, a)
    character(len=*), intent(in) :: title
    integer, intent(in) :: a(:, :)
    integer :: r
    print '(a)', title // ':'
    do r = 1, size(a, 1)
      print '(*(i4))', a(r, :)
    end do
  end subroutine show

end program sections_2d
grid:
   1   5   9  13  17
   2   6  10  14  18
   3   7  11  15  19
   4   8  12  16  20
rows 2-3, cols 2-4:
   6  10  14
   7  11  15
every other column:
   1   9  17
   2  10  18
   3  11  19
   4  12  20
column 5 as a vector:  17  18  19  20
first row cleared:
   0   0   0   0   0
   2   6  10  14  18
   3   7  11  15  19
   4   8  12  16  20

The helper show is an internal subroutine (after contains). Its argument a(:, :) accepts a 2-D array of any shape, including sections. Procedures are covered in detail later; for now, treat it as a reusable printing helper.

reshape(source, shape) fills an array of the given shape from a flat list, again in column-major order. It's covered with the other matrix functions shortly.

A 2-D example: a temperature grid

fortran · try it yourself
program heat_grid
  implicit none
  real :: t(5, 5)
  integer :: i

  t = 20.0              ! room temperature everywhere
  t(1, :) = 100.0       ! hot top edge
  t(5, :) = 0.0         ! cold bottom edge

  ! one smoothing step: each interior point becomes the mean of its 4 neighbours
  t(2:4, 2:4) = 0.25 * (t(1:3, 2:4) + t(3:5, 2:4) + t(2:4, 1:3) + t(2:4, 3:5))

  do i = 1, 5
    print '(5f7.1)', t(i, :)
  end do
end program heat_grid
  100.0  100.0  100.0  100.0  100.0
   20.0   40.0   40.0   40.0   20.0
   20.0   20.0   20.0   20.0   20.0
   20.0   15.0   15.0   15.0   20.0
    0.0    0.0    0.0    0.0    0.0

Because the right-hand side is evaluated completely before assignment, every new value uses the old neighbours, exactly as the numerical method (Jacobi iteration) requires.

Test yourself

In memory, which element follows a(1, 1) in a Fortran array a(3, 3)?

  1. a(1, 2)
  2. a(2, 1)
  3. a(3, 3)
  4. a(2, 2)

Which loop order is fastest for a(i, j) on a large array?

  1. Outer loop over i, inner over j
  2. Outer loop over j, inner over i
  3. Both are identical
  4. It depends on the values

What is shape(x) for real :: x(2, 7)?

  1. 14
  2. [2, 7]
  3. [7, 2]
  4. 2