|
|
| Re: array indexing question [message #31438 is a reply to message #31433] |
Thu, 11 July 2002 18:20  |
mperrin+news
Messages: 81 Registered: May 2001
|
Member |
|
|
Michael A. Miller <mmiller3@iupui.edu> wrote:
> I have three arrays, pa, pb and pab. pa and pb are both 1D
> arrays of length N and pab is a 2D NxN array. I want to
> calculate the sum of pab[i,j]*alog(pab[i,j])/pa[i]/pb[j]. I know
> that I can do things like total( pa * alog(pa) ) when I'm dealing
> with a single array. Any suggestions for how to do my first sum
> most efficiently?
How about just
total( pab * alog(pag) / rebin(pa,N,N) / rebin(transpose(pb),N,N))
The reform() calls turn your 1D arrays into 2D arrays, so they can
then easily be combined with the pab array. The transpose call is
needed to get the latter vector changed into a column rather than
row vector so the indices work properly.
See the ever-popular
http://www.dfanning.com/tips/rebin_magic.html
for more tricks along these lines.
- Marshall
|
|
|
|