In a Fortran code, I have an array a
with values ranging uniformly from 0.0
to 1.2
and I am looking for the index of the value which is closest to 1.0
.
There don't seem to be any intrinsics to do this in Fortran, so I made a hacky workaround with MINLOC: (note that temp
is the same shape as a
)
do i=1,n
temp(i) = 1.0 - a(i)
end do
loc = 0 !note that loc is an integer array, declared as 'loc(1)'
index_1 = minloc(abs(temp(:))) !this is the index of a closest to 1.0
This seems to do the trick.
I'm interested in other methods to achieve this, so have a few questions:
- How does MINLOC work?
- Are there other routines to just get straight there (i.e. without having to make a second array)?
- How is this handled in other languages?
Thanks.
EDIT: I previously linked to this article, citing it as partial motivation to finding alternatives to MINLOC
, but have removed it as some commenters have pointed out it is somewhat irrelevant. Including it seemed to be detracting from my aim which is to find alternatives to this method.