Silverfrost Forums

Welcome to our forums

minloc function

29 Sep 2014 7:18 #14738

I'd like to find the position of the smallest element in a vector, and I tried this:

      winapp 
      program test 
      implicit none
      include <windows.ins> 

      integer*4      i
      real*8         r(4)

      r(1) = 0.D0
      r(2) = 7.2D0
      r(3) = -.8D0
      r(4) = 0.D0

      i = minloc(r)
      print*,i,r(i)   ! should give 3, -.8D0

      end

It failes with the message 'Result has insufficient elements'. Can anyone please help?

Thanks & regards Wilfried

29 Sep 2014 10:00 #14741

I've seen this one before, i must be dimensioned as a vector with one element.

       winapp 
       program test 
       implicit none 
       include <windows.ins> 

       integer*4      i(1)    !NOTE:: must be dimensioned.
       real*8         r(4) 

       r(1) = 0.D0 
       r(2) = 7.2D0 
       r(3) = -.8D0 
       r(4) = 0.D0 

       i = minloc(r) 
       print*,i,r(i)   ! should give 3, -.8D0 

       end

Cheers Ken

29 Sep 2014 10:24 #14742

Ken, thanks a lot, now it works!

Wilfried

29 Sep 2014 4:57 #14748

Yes. In the original code, the result is an array of rank 1.

Alternatively, you can use the DIM argument to get a scalar result. So the original code becomes.

      winapp
      program test
      implicit none
      include <windows.ins>

      integer*4      i
      real*8         r(4)

      r(1) = 0.D0
      r(2) = 7.2D0
      r(3) = -.8D0
      r(4) = 0.D0

      i = minloc(r, dim=1)
      print*,i,r(i)   ! should give 3, -.8D0

      end
30 Sep 2014 7:21 #14752

Thanks again!

I was a bit confused because the related functions minval and maxval don't need such constructions but simply run with j = minval(r) or j = maxval(r).

Have a nice day Wilfried

30 Sep 2014 7:53 #14753

It puzzles me also, although if the array being scanned is of rank > 1 then the result is an array of the same rank and of size = rank. Extrapolating this for a scanned array of rank 1 is a bit of an anomaly.

You'd think it would have been easier in this case to allow either I = minloc(array) or I(1) = minloc(array) to be acceptable.

this might be explained as array = scalar ! is acceptable but scalar = array ! is not ok

Just another tick in the box of avoiding this intrinsic function, which looks inefficient, especially for large arrays.

John

Please login to reply.