Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Get location of value in array

alexismor
Beginner
785 Views
Hi everybody,

This is probably a very dumb question, but I'm just getting started with Fortran (and programming in general). Say I have an array of integers, which are randomly filled. What's the best way to get all the array indicies which hold a certain value? For example, I have:

integer :: A(4)
A(1)=0
A(2)=1
A(3)=5
A(4)=0

I want to know every i where A(i)=0. I could do a loop, but I was wondering if there's a better way (ie, faster) way to do this. I found the commands maxloc, minloc, but these only give the position of one of the values.

Is there something like:

loc(A(i)==0)

which would give me an array containing the indicies 1 and 4?

More specifically, my problem is that I have a derived type called particles, which contain various quantum numbers for each particle:

type particle
integer :: n,m,s
end type particle

I then have an array of these particles and want to find all "m"'s quantum numbers of particles which have "n"=0. This has to be done millions of times, so I'd like to know the fastest way of implementing this.

Thanks, and sorry for the long post!

Cheers,

Alexis

Message Edited by alexismor on 05-04-2005 06:20 PM

0 Kudos
4 Replies
Paul_Curtis
Valued Contributor I
785 Views
If the number of different quantum numbers is small enough, and their values restricted to small integers, you might consider using sets of bits for each quantum number, assembled into aninteger-like value representing the overall status of the particle(ie, one 32-bit integer could hold 4ea 8-bit subfields, or 8ea 4-bit subfields). This would permit using bit-masking and comparison operators to sort through your particles, which are very fast, and you can easily construct functions to set or clear the various defined subranges.
0 Kudos
alexismor
Beginner
785 Views
Hi Paul-Curtis,

Thanks for the reply. The number of quantum numbers for my particles is just 3 (n,m,s) so I'll definitively look into your suggestion. I've never done something like that, but it sounds very interesting. Thanks!

Alexis
0 Kudos
Steven_L_Intel1
Employee
785 Views
Use the PACK intrinsic. It returns an array containing only those elements which match a mask. An example:

integer, dimension(5) :: a1 = (/3,1,4,1,5/)

print *,pack(a1,a1==1)
end

Running this, prints:
           1           1
0 Kudos
Steven_L_Intel1
Employee
785 Views
You can do it with two intrinsics, MERGE and PACK. See the following example:

integer, dimension(5) :: a1 = (/3,1,4,1,5/)
integer, dimension(5), parameter :: indexes = (/(i,i=1,5)/)
integer, dimension(5), parameter :: zeroes = 0
integer, dimension(5) :: temp

! Create an array where an element is the element position, if
! the value is found, or zero
temp = merge (indexes, zeroes, a1==1)

! Strip out the zeroes
print *,pack(temp,temp/=0)
end
0 Kudos
Reply