Previous Table of Contents Next


1.2.15.3. Array Sections

Sometimes, only a portion of the elements of an array is needed for a computation. It is possible to refer to a selected portion of an array, called an array section.

For example, if v is a one-dimensional array of ten numbers, then

   v (0:4)

represents elements v(0), v(1), v(2), v(3), and v(4) and

   v (3:7:2)

represents elements v(3), v(5), and v(7).

Another way of selecting a section of an array is to use a vector subscript. A vector subscript is an integer array expression of rank one. For example, if iv is an array of three integers, 3, 7, and 2, and x is an array of 9 real numbers 1.1, 2.2, … 9.9, the value of x(iv) is the list of three numbers 3.3, 7.7, and 2.2—the third, seventh, and second elements of x.

1.2.15.4. Array Assignment

Array assignment is permitted under two circumstances: when the array expression on the right has exactly the same shape as the array on the left, and when the expression on the right is a scalar. The term for this is that the expression on the right of the equals is conformable to the variable on the left. Note that, for example, if a is a 9 × 9 array, the section a(2:4,5:8) is the same shape as a(3:5,1:4), so the assignment

   a (2:4, 5:8) = a (3:5, 1:4)

is valid, but the assignment

   a (1:4, 1:3) = a (1:3, 1:4)

is not valid because even though there are 12 elements in the array on each side of the assignment, the left side has shape (4,3) and the right side has shape (3,4).

When a scalar is assigned to an array, the value of the scalar is assigned to every element of the array. Thus, for example, the statement

   m (k+1:n, k) = 0

sets the elements m(k+1,k), m(k+2,k), … m(n,k) to 0.

1.2.15.5. Intrinsic Operators

All of the intrinsic operators and many of the intrinsic functions may be applied to arrays, operating independently on each element of the array. For example, the expression abs(a(k:n,k)) results in a one-dimensional array of n-k+1 non-negative real values. A binary operation, such as *, may be applied only to two arrays of the same shape or an array and a scalar. It multiplies corresponding elements of the two arrays or multiplies the elements of the array by the scalar. The assignment statement

   a (k, k:n+1) = a (k, k:n+1) / pivot

divides each element of a(k,k:n+1) by the real scalar value pivot. In essence, a scalar value may be considered an array of the appropriate size and shape with all its entries equal to the value of the scalar.

1.2.15.6. Masked Array Assignment—The where Construct

The where construct may be used to assign values to only those elements of an array where a logical condition is true. For example, the following statement sets the elements of b to 0 in those positions where the corresponding element of a is negative:

   where (a < 0)
    b = 0
   end where

The other elements of b are unchanged. a and b must be arrays of the same shape. The logical condition in parentheses is an array of logical values conformable to each array in the assignment statement. In the preceding example, comparison of an array of values with a scalar produces the array of logical values.

The where construct permits any number of array assignments to be done under control of the same logical array, and the elsewhere statement within a where construct permits array assignments to be done where the logical expression is false. The following statements assign to the integer array size_category an indication of the size of the elements of b:

   real, dimension (m,n) :: b
   integer, dimension (m,n) :: a
      . . .
   where (abs(b) > huge (b)/100.0)
      a = 3
   elsewhere (abs(b) > 10.0*epsilon (b)
      a = 2
   elsewhere
      a = 1
   end where

Within a where construct, only array assignments and nested where constructs are permitted. The shape of all arrays in the assignment statements must conform to the shape of the logical expression following the keyword where. The assignments are executed in the order they are written—first those in the where block, then those in the elsewhere block.


Previous Table of Contents Next