Re: element location in a vector ?



"AM" <wiredashu@xxxxxxxxx> wrote in message
news:1135265163.311629.79140@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

> I was wondering if anyone else might have discovered a quick way of
> locating the position of an element in a vector ? Something akin to the
> "find' function in MATLAB. The only intrinsic functions i found - that
> had to do with location in a vector were:
> MAXLOC/MINLOC.

Well, 'find' returns an array of indices. Something like

I = find(X == 42);

Could be replaced by

integer, allocatable :: I(:)
....
allocate(I(count(X == 42))
I = pack((/(j,j=lbound(X,1),ubound(X,1))/),X == 42)

Instead of the two-step approach with ALLOCATE, one
could capture the result of PACK as an assumed-shape
dummy argument. Fortran doesn't need 'find' to the
same extent that Matlab does because it has MASK
optional arguments to many transformational intrinsics,
a MERGE intrinsic, a WHERE statement and construct,
and a mask-expr possible in the FORALL statement and
construct. You might wish to write a FIND function
yourself; it would look something like:

function find(X)
logical, intent(in) :: X(:)
integer find(count(X))
integer j

find = pack((/(j,j=1,size(X))/),X)
end function find

The above requires an explicit interface, of course.

--
write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, &
6.0134700243160014d-154/),(/'x'/)); end


.