# MAXVAL

Transformational Intrinsic Function (Generic): Returns the maximum value of all elements in an array, a set of elements in an array, or elements in a specified dimension of an array.

result = MAXVAL (array [, mask])

result = MAXVAL (array, dim [, mask])

array | (Input) Must be an array of type integer, real, or character.
dim | (Input) Must be a scalar integer expression with a value in the range 1 to n, where n is the rank of array.
mask | (Input; optional) Must be a logical array that is conformable with array.

## Results

The result is an array or a scalar of the same data type as array.

The result is a scalar if dim is not specified or array has rank one.

The following rules apply if dim is not specified:

- If MAXVAL( array) is specified, the result has a value equal to the maximum value of all the elements in array.

- If MAXVAL( array, MASK= mask) is specified, the result has a value equal to the maximum value of the elements in array corresponding to the condition specified by mask.

The following rules apply if dim is specified:

- An array result has a rank that is one less than array, and shape (d1, d2,...,ddim-1, ddim+1, ..., dn), where (d1, d2, ..., dn) is the shape of array.

- If array has rank one, MAXVAL( array, dim[, mask]) has a value equal to that of MAXVAL( array[,MASK = mask]). Otherwise, the value of element (s1, s2, ..., sdim-1, sdim+1, ..., sn) of MAXVAL( array, dim, [, mask]) is equal to MAXVAL( array(s1, s2, ..., sdim-1, :, sdim+1, ..., sn) [,MASK = mask(s1, s2, ..., sdim-1, :, sdim+1, ..., sn)]).

If array has size zero or if there are no true elements in mask, the result (if dim is omitted), or each element in the result array (if dim is specified), has the value of the negative number of the largest magnitude supported by the processor for numbers of the type and kind parameters of array.

If array is of type character, the comparison is done using the ASCII collating sequence.

## Example

The value of MAXVAL ((/2, 3, 4/)) is 4 because that is the maximum value in the rank-one array.

MAXVAL (B, MASK=B .LT. 0.0) finds the maximum value of the negative elements of B.

C is the array

      [ 2  3  4 ]
      [ 5  6  7 ].

MAXVAL (C, DIM=1) has the value (5, 6, 7). 5 is the maximum value in column 1; 6 is the maximum value in column 2; and so forth.

MAXVAL (C, DIM=2) has the value (4, 7). 4 is the maximum value in row 1 and 7 is the maximum value in row 2.

The following shows another example:

     INTEGER array(2,3), i(2), max
     INTEGER, ALLOCATABLE :: AR1(:), AR2(:)
     array = RESHAPE((/1, 4, 5, 2, 3, 6/),(/2, 3/))
     ! array is   1 5 3
     !            4 2 6
     i = SHAPE(array)      ! i = [2 3]
     ALLOCATE (AR1(i(2)))  ! dimension AR1 to the number of
                           ! elements in dimension 2
                           ! (a column) of array
     ALLOCATE (AR2(i(1)))  ! dimension AR2 to the number of
                           ! elements in dimension 1
                           ! (a row) of array
     max = MAXVAL(array, MASK = array .LT. 4) ! returns 3
     AR1 = MAXVAL(array, DIM = 1)  ! returns [ 4 5 6 ]
     AR2 = MAXVAL(array, DIM = 2)  ! returns [ 5 6 ]
     END

Parent topic: M to N

## See Also

MAXLOC
MINVAL
MINLOC