View previous topic :: View next topic |
Author |
Message |
Wilfried Linder
Joined: 14 Nov 2007 Posts: 314 Location: D�sseldorf, Germany
|
Posted: Mon Sep 29, 2014 8:18 am Post subject: minloc function |
|
|
I'd like to find the position of the smallest element in a vector, and I tried this:
Code: | 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 |
|
Back to top |
|
 |
Kenneth_Smith
Joined: 18 May 2012 Posts: 814 Location: Hamilton, Lanarkshire, Scotland.
|
Posted: Mon Sep 29, 2014 11:00 am Post subject: |
|
|
I've seen this one before, i must be dimensioned as a vector with one element.
Code: | 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 |
|
Back to top |
|
 |
Wilfried Linder
Joined: 14 Nov 2007 Posts: 314 Location: D�sseldorf, Germany
|
Posted: Mon Sep 29, 2014 11:24 am Post subject: |
|
|
Ken, thanks a lot, now it works!
Wilfried |
|
Back to top |
|
 |
davidb
Joined: 17 Jul 2009 Posts: 560 Location: UK
|
Posted: Mon Sep 29, 2014 5:57 pm Post subject: |
|
|
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.
Code: |
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
|
_________________ Programmer in: Fortran 77/95/2003/2008, C, C++ (& OpenMP), java, Python, Perl |
|
Back to top |
|
 |
Wilfried Linder
Joined: 14 Nov 2007 Posts: 314 Location: D�sseldorf, Germany
|
Posted: Tue Sep 30, 2014 8:21 am Post subject: |
|
|
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 |
|
Back to top |
|
 |
JohnCampbell
Joined: 16 Feb 2006 Posts: 2615 Location: Sydney
|
Posted: Tue Sep 30, 2014 8:53 am Post subject: |
|
|
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 |
|
Back to top |
|
 |
|