MAXLOC, MINLOC and FINDLOC
The reductions of the earlier lesson answer what a value is; the locators answer where it sits. maxloc and minloc give the position of the extreme, findloc gives the position of a value the program names. All three take the mask and the dimension keywords a reduction takes, and all three return a number that is an index into the array, not a count.
MAXLOC and MINLOC
For a rank-one array the result is a scalar: the position of the largest or the smallest element, counted from the lower bound, which is one for an array declared the usual way.
The file locate.f90:
program locate_demo
implicit none
integer :: a(7)
a = [3, 8, 1, 9, 2, 9, 7]
print *, "a :", a
print *, "maxval :", maxval(a), " at ", maxloc(a)
print *, "minloc :", minloc(a)
print *, "last 9 :", maxloc(a, back=.true.)
print *, "max > 8 :", maxloc(a, mask=a > 8)
end program locate_demo
gfortran -Wall -Wextra -std=f2018 locate.f90 -o locate
./locate
a : 3 8 1 9 2 9 7
maxval : 9 at 4
minloc : 3
last 9 : 6
max > 8 : 4
The largest value is nine and it appears twice, at four and at six. Without a keyword the answer is the first one, which is the rule of the language and not a property of this compiler.
First or last
back=.true. asks for the last match instead of the first, which is what a program wants when it reads a file backwards or looks for the most recent of a repeated value. The default is back=.false., so the keyword is only written when the answer must be the last one.
The same keyword is on findloc and on the index intrinsic for strings, so the habit carries across the language.
A mask in the locator
maxloc(a, mask=a > 8) searches only the elements the mask allows. If the mask selects nothing the result is zero, which is not a valid index: the test is count(mask) > 0 before the locator is used, exactly as with a masked reduction.
A mask is the way to ask a question about part of the data: the position of the largest negative value is maxloc(a, mask=a < 0), and the position of the earliest value above a threshold is findloc(a, .true., mask=a > threshold) or, more directly, minloc(a, mask=a > threshold).
Positions in a matrix
On a matrix the locator returns as many subscripts as the array has dimensions, as a rank-one array: maxloc(m) is a pair, and a program reads it as loc(1) and loc(2). With dim= it returns the positions along the reduced dimension, one per line or column.
The file dimloc.f90 uses a three by four matrix:
program dimloc_demo
implicit none
integer :: m(3, 4)
m = reshape([5, 9, 2, 4, 8, 1, 7, 3, 6, 2, 10, 0], [3, 4])
print *, "m :", m
print *, "maxval :", maxval(m)
print *, "maxloc :", maxloc(m)
print *, "col maxima :", maxval(m, dim=1)
print *, "col maxloc :", maxloc(m, dim=1)
print *, "row maxima :", maxval(m, dim=2)
end program dimloc_demo
gfortran -Wall -Wextra -std=f2018 dimloc.f90 -o dimloc
./dimloc
m : 5 9 2 4 8 1 7 3 6 2 10 0
maxval : 10
maxloc : 2 4
col maxima : 9 8 7 10
col maxloc : 2 2 1 2
row maxima : 7 10 6
The ten sits in row two, column four, so the pair is 2 4 and the largest of each column is 9, 8, 7 and 10 in the positions 2, 2, 1 and 2 of their columns. The row maxima need dim=2 because they reduce the second index, and they come out one per row.
FINDLOC
findloc(array, value) is the locator for a value the program chooses, and it is the one a search loop becomes. It compares with ==, so for reals it has the problem of every exact comparison; for strings and integers it is exactly what a table lookup needs.
program findloc_demo
implicit none
integer :: a(8)
a = [4, 7, 1, 7, 9, 2, 7, 5]
print *, "a :", a
print *, "find 7 :", findloc(a, 7)
print *, "find back:", findloc(a, 7, back=.true.)
print *, "find > 5 :", findloc(a, 7, mask=a > 5)
print *, "none :", findloc(a, 42)
end program findloc_demo
gfortran -Wall -Wextra -std=f2018 findloc.f90 -o findloc
./findloc
a : 4 7 1 7 9 2 7 5
find 7 : 2
find back: 7
find > 5 : 2
none : 0
The value seven appears three times, at two, four and seven: the first search gives two and the backward one seven. The masked search skips the seven at position four because the mask is false there, so the answer is the seven at two. A value that is not in the array gives zero, which is the value to test before using the result as a subscript.
findloc arrived in Fortran 2008. A program that must build with an older standard writes the loop, and maxloc(a, mask=a == value) is the rank-one substitute that has been there since Fortran 90.
Exercises
- Print the position of the smallest value above three in a. Which keywords do you need?
- Ask for maxloc(a, mask=a < 0) on the seven values. What comes out, and why is it not a subscript?
- Print row and column of the smallest value of the matrix with minloc. Check the pair against the printed matrix.
- Search for a value that appears twice with findloc, once forwards and once backwards. Which positions do the two calls give?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
maxloc(a)return?Answer
The position of the largest element
The locator returns an index; maxval returns the value.
-
What does
maxloc(a, back=.true.)return when the largest value appears twice?Answer
The position of the last occurrence
BACK asks for the last match.
-
What does this program print?
program p implicit none integer :: a(4) a = [5, 1, 9, 9] print *, maxloc(a, back=.true.) end program pAnswer
4
The largest value is at three and four, and BACK takes four.
-
What does a locator return when its mask selects nothing?
Answer
Zero
Zero is the no-match value, and it is not a valid subscript.
-
What is
maxloc(m)for a rank-two matrix?Answer
A rank-one array with the two subscripts of the largest element
One subscript per dimension.
-
What does
maxloc(m, dim=1)return for a matrix?Answer
The row index of the largest value in each column
Reducing the first index leaves one position per column.
-
What does
findloc(a, 42)return when 42 is not in the array?Answer
0
Zero marks "not found".
-
Which intrinsic finds the position of a value the program names?
Answer
findloc
findloc takes the value as its second argument.
-
What does
maxloc(a, mask=a > 8)give for a = [3, 8, 1, 9, 2, 9, 7]?Answer
4
Only the two nines pass the mask, and the first is at four.
-
Which keyword makes FINDLOC search for the last match?
Answer
BACK=.TRUE.
BACK is the shared keyword of the locators.
The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev