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

Home » Public Forums » archive » Incrementing an element multiple times without a loop?
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Incrementing an element multiple times without a loop? [message #88440] Fri, 25 April 2014 07:50 Go to previous message
TimB is currently offline  TimB
Messages: 6
Registered: April 2011
Junior Member
I'm trying to speed up a process within a nested loop. Essentially I have an array of 0's (array a) and another array which tells which elements of a to increment by +1 (array b).

The element in array a should be incremented the same number of times it is in array b.

For example a = [0,0,0], b=[1,1,2], the result should be [0,2,1]
Simply using a[b]+=1 only increments the each unique element in b once and would give [0,1,1]

The only thing I could come up was
result=uniq(b[sort(b)])
result-=[-1,result[0:n_elements(b)-2]]

but that only works if all of the elements in a are mentioned in b at least once. Here's a small example:



PRO ele_increment

a=intarr(5) ;blank array
b=[0,1,0,3,2,4,4,1,1,2,0,1,1] ;elements in arr to increment by +1

;using loop
for i=0,n_elements(b)-1 do a[b[i]]+=1

;no loop
result=uniq(b[sort(b)])
result-=[-1,result[0:n_elements(result)-2]]

print,arr
print,result

END



This works but if I was to remove the 3 from the b array, the result is no longer correct.

Any ideas or thoughts?

Thanks, Tim
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Previous Topic: H5D_Read on compound type (GDL)
Next Topic: Problem reading HDF5

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

Current Time: Wed Oct 08 11:42:25 PDT 2025

Total time taken to generate the page: 0.00378 seconds