"Jeff Newmark" <newmark@midas.nrl.navy.mil> wrote in message
news:3C5ED8D5.25218FAA@midas.nrl.navy.mil...
> I have a nested loop, theoretically each limit is 1000.
> Obviously, I do not wish to have 1 million iterations
> here. Any suggestions on how to get rid of one of
> the loops?
So basically, you have
dat, i, j, mu, nu, r2, rs2: all fltarr(n, n)
image: fltarr(n, n, n)
and supposedly also:
N, scalar: longints, less then or equal to n
fa: fltarr(?)
correct me if i'm wrong...
you want:
for a=0,N-1 do $
for b=0,N-1 do $
dat[ i[a,b], j[a,b] ] = $
dat[ i[a,b], j[a,b] ] + image[ mu[a,b], nu[a,b], scalar] * fa[r2[a,b]]
/ rs2[a,b]
off-hand, I would write (untested):
slice = image[*, *, scalar]
dat[i, j] = dat[i, j] + slice[mu, nu] * fa[r2] / rs2
Now if i or j contain duplicate elements (duplicated within themselves,
after they have been converted to lonarr), you are probably in for some real
fun.
The above trick will skip the duplicate elements. Histogram springs to mind
as the obvious (well....) path to a solution, but the example given in the
online
help is quite a bit more primitive than your problem: dat[i] = dat[i] + 1
...
Anyway, histogram is my suggestion. Just to kick off the thread; maybe
someone
else can comment on the exact syntax? :-)
cheers,
Jaco
P.S.: I just googled upon this very nice thread on the problem of dat[i] =
dat[i] + arr ,
with arr an array dimensionally equal to i. This should translate quite
nicely to the
problem at hand. Solutions mentioned were a memory-hungry 2D-trick, a neat
little
trick picking elements out of a cumulative TOTAL, a creative use of
HISTOGRAM
(looping over the number of hits!), and an external routine in FORTRAN. Now
that's four
suggestions, all of them equally mind-dazzling. The thread is called
"Vectorization question", somewhere in september 2000.
|