comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Sum to Arrays
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Sum to Arrays [message #4243] Thu, 11 May 1995 00:00 Go to next message
phrje is currently offline  phrje
Messages: 4
Registered: October 1993
Junior Member
We wish to e.g. sum the columns of a 2d array and store the results in a
1d array. Is there any way to accomplish this without using FOR loops.

Don
Re: Sum to Arrays [message #4309 is a reply to message #4243] Tue, 23 May 1995 00:00 Go to previous message
mole is currently offline  mole
Messages: 5
Registered: April 1995
Junior Member
Stein Vidar Hagfors Haugan (steinhh@amon.uio.no) wrote:

: 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))
: |>

oops... I meant:

x = fltarr(n)
for i=0,n-1 do x(i) = TOTAL(y(*,i) * findgen(m))

get the idea? Weight each column of y by its index and then total

also... I'm trying to be portable between PV-Wave and IDL, so I cannot
use the 2 argument total. (I just found that out)


: 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)

neat. I doubt I'd have come up with that.

: 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...

thanks.
--
Aaron Birenboim | 1007 Pampas Dr. SE |
mole@netcom.com | Albuquerque, NM 87108 |
(505) 254-1749 | |
(505) 247-8371 (w) or birenboi@ataway.aptec.com |
Re: Sum to Arrays [message #4336 is a reply to message #4243] Sat, 20 May 1995 00:00 Go to previous message
steinhh is currently offline  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
Re: Sum to Arrays [message #4342 is a reply to message #4243] Fri, 19 May 1995 00:00 Go to previous message
mole is currently offline  mole
Messages: 5
Registered: April 1995
Junior Member
Fergus Gallagher (F.Gallagher@nerc.ac.uk) wrote:
: phrje@csv.warwick.ac.uk (Prof. Don McK Paul) wrote:
: >We wish to e.g. sum the columns of a 2d array and store the results in a
: >1d array. Is there any way to accomplish this without using FOR loops.
: >
: >Don

: The total function has an optional second argument, the dimension to sum
: over, e.g.,

: IDL> x = total(y,2)

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???

--
Aaron Birenboim | 1007 Pampas Dr. SE |
mole@netcom.com | Albuquerque, NM 87108 |
(505) 254-1749 | |
(505) 247-8371 (w) or birenboi@ataway.aptec.com |
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: object searching in an image
Next Topic: complex math error?

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 19:17:01 PDT 2025

Total time taken to generate the page: 0.00781 seconds