Re: Sum to Arrays [message #4336 is a reply to message #4243] |
Sat, 20 May 1995 00:00   |
steinhh
Messages: 260 Registered: June 1994
|
Senior Member |
|
|
In article <moleD8uArz.DFG@netcom.com>, mole@netcom.com (Aaron Birenboim) writes:
[...stuff deleted...]
|>
|> OK.... here's a tougher one to vectorize...
|>
|>
|> I want CENTROIDS, FAST!!! I'm stuck with :
|>
|> x = fltarr(m)
|> for i=0,n do x = TOTAL(y(*,i) * findgen(n+1))
|>
|> any ideas???
|>
I don't get it. Why x=fltarr(m), and then set x equal to a scalar n+1
times over? Why findgen(n+1) ?
Anyway, here are centroids for you:
Given e.g., I(x,y), an M x N array:
X = REBIN(REFORM(FINDGEN(M),M,1,/OVERWRITE),M,N,/SAMPLE) ; Gives you X(x,y)
CTROIDS_X = TOTAL(I*X,1)/TOTAL(I,1) ; Gives an array of the centroids
; (the X centroid at each y index.)
CTROID_X = TOTAL(I*X)/TOTAL(I) ; Gives the X Centroid coordinate
; of the whole array.
;------------ or, for Y centroids
Y = REBIN(REFORM(FINDGEN(N),1,N,/OVERWRITE),M,N,/SAMPLE) ; Gives you Y(x,y)
CTROIDS_Y = TOTAL(I*Y,2)/TOTAL(I,2) ; Gives an array of Y centroids...
CTROID_Y = TOTAL(I*Y)/TOTAL(I) ; Gives the Y Centroid.
All vectorized. For some types of applications you should
add something small before dividing with the TOTAL(I,..), to
avoid division with zero, e.g., ...../(TOTAL(I,1)+1e-22)
Stein Vidar
|
|
|