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

Home » Public Forums » archive » histogram question
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: Histogram question [message #40376 is a reply to message #26139] Sun, 08 August 2004 11:28 Go to previous messageGo to previous message
Michael Wallace is currently offline  Michael Wallace
Messages: 409
Registered: December 2003
Senior Member
> I don't know if this is the most "efficient" way,
> but the idea is that you have to "replicate" the
> numbers in v1 and v2 by the number of counts in v3.
>
> For a quick and dirty method, I used this:
>
> ;*********************************************************** ******
> v1 = [0, 1, 0, 2, 0, 2, 2, 1, 0]
> v2 = [1, 1, 2, 2, 0, 1, 2, 0, 0]
> v3 = [3, 0, 2, 0, 1, 1, 4, 2, 1]
>
> ; Replicate each index by the number of counts.
>
> v1_expand = Ptr_New(/Allocate_Heap)
> v2_expand = Ptr_New(/Allocate_Heap)
>
> *v1_expand = [Replicate(v1[0], v3[0])]
> *v2_expand = [Replicate(v2[0], v3[0])]
> FOR j=1,N_Elements(v3)-1 DO BEGIN
> IF v3[j] NE 0 THEN *v1_expand = [*v1_expand, Replicate(v1[j], v3[j])]
> IF v3[j] NE 0 THEN *v2_expand = [*v2_expand, Replicate(v2[j], v3[j])]
> ENDFOR
>
> final = Hist_2D(*v1_expand, *v2_expand)
> Ptr_Free, v1_expand, v2_expand
> Print, final
> ;*********************************************************** ******
>
> Which gave me the answer:
>
> 2 2 0
> 3 0 1
> 2 0 4
>

Thanks, David. While this does work, using replicate could potentially
create some monster-sized arrays. I failed to mention before that in a
normal case, the values in the v3 vector will be on the order of
thousands... and sometimes 10s of thousands. That's going to be a lot
of numbers in memory!

I wrote this little snippet which doesn't use HIST_2D at all. It's just
a simple FOR loop and adding values. Back in the good ol' days, I would
have been satisfied with this, but after having read this newsgroup for
a while I just have a feeling that there's a way to get rid of that FOR
loop and do something totally cool with the HISTOGRAM function. So I
guess my question is more academic than anything else. Anyway, here's
the snippet ...


v1 = [0, 1, 0, 2, 0, 2, 2, 1, 0]
v2 = [1, 1, 2, 2, 0, 1, 2, 0, 0]
v3 = [3, 0, 2, 0, 1, 1, 4, 2, 1]


arr = intarr(max(v1) + 1, max(v2) + 1)
n = n_elements(v3)

for i = 0, n - 1 do begin
arr[v1[i], v2[i]] += v3[i]
endfor


print, arr


-Mike
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Contour Border Sort Error
Next Topic: Passing Structures with Pointers with Call_External

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

Current Time: Sun Oct 12 11:22:01 PDT 2025

Total time taken to generate the page: 1.19933 seconds