Home > Archive > Fortran > March 2004 > Question about minloc
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Question about minloc
|
|
| Douglas Sondak 2004-03-27, 12:16 am |
| In the following code, why is it necessary to specify dim=1
when using minloc on array "a," but not on array "b"? (I'm
compiling with xlf95 under AIX 7.1.1.4.)
program test_minloc
implicit none
integer :: i, m
integer, dimension(2,2) :: &
a = reshape((/4,3,-1,5/),(/2,2/))
integer, dimension(3) :: b = (/1, -1, 2/)
do i = 1, 2
m = minloc(a(i,:),dim=1)
print*,'i,m = ', i,m
enddo
print*,'minloc(b) = ', minloc(b(:))
end program test_minloc
| |
| Rich Townsend 2004-03-27, 12:17 am |
| Douglas Sondak wrote:
> In the following code, why is it necessary to specify dim=1
> when using minloc on array "a," but not on array "b"? (I'm
> compiling with xlf95 under AIX 7.1.1.4.)
>
> program test_minloc
> implicit none
> integer :: i, m
> integer, dimension(2,2) :: &
> a = reshape((/4,3,-1,5/),(/2,2/))
> integer, dimension(3) :: b = (/1, -1, 2/)
> do i = 1, 2
> m = minloc(a(i,:),dim=1)
> print*,'i,m = ', i,m
> enddo
> print*,'minloc(b) = ', minloc(b(:))
> end program test_minloc
>
Because the MINLOC() intrinsic returns a 1-D array of minimum indices,
*unless* the optional DIM argument is present -- in which case, it
returns the scalar minimum index for the specified dimension.
When you do
m = minloc(a(i,:),dim=1),
if the DIM argument *weren't* present, then (apart from your program
*logic* going awry) your program would contain an error: that of trying
to assign an array to a scalar. In this specific case, the array only
has one element; but it is still an array, and therefore not assignable
to a scalar.
In the case of
print*,'minloc(b) = ', minloc(b(:)),
even though only a single number is printed out, this single number
represens the entirety of a 1-D array -- it is not a scalar. Therefore,
doing something like
m = MINLOC(b)
would cause the same problems of trying to assign an array to a scalar.
However, the PRINT statement doesn't care whether it gets an array or a
scalar, therefore no problems can arise by using the DIM-less MINLOC()
with PRINT.
cheers,
Rich
PS IIRC, the DIM argument to MINLOC is a Fortran 95 feature, and a very
useful one at that; in Fortran 90, nasty kludges like SUM(MINLOC(b))
were necessary when finding scalar minimum/maximum indices.
|
|
|
|
|