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

Home » Public Forums » archive » Count duplicate elements in an array but keep their order!
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
Count duplicate elements in an array but keep their order! [message #85750] Fri, 30 August 2013 16:23 Go to next message
Steve Daal is currently offline  Steve Daal
Messages: 13
Registered: October 2011
Junior Member
Hi,

I have a long array that looks like:
a =[
-120.09796
-120.09796
-108.18972
-108.18972
-108.18972
-121.98
-95.23
-95.23
-95.23
-95.23
-95.23
-95.23
-95.23
-95.23
-95.23
-95.23
-95.23
-95.23
-95.23
-95.03
-95.03
-95.03
-95.03
-71.303756
-122.39084
-122.39084
-122.39084
-122.24503]

with many more elements inside. I need to create two new arrays with the consecutive counts corresponding to the values of the first array after removing the duplicates, for instance, the above array would give me:
a_new = [-120.09796, -108.18972, -121.98, -95.23, -71.303756, -122.24503]
and
b_new = [2, 3, 1, 13, 1, 4]

where, elements -120.09796" existed twice in the original array and so forth.
Any help is much appreciated.
Re: Count duplicate elements in an array but keep their order! [message #85751 is a reply to message #85750] Sat, 31 August 2013 08:03 Go to previous messageGo to next message
Josh Sixsmith is currently offline  Josh Sixsmith
Messages: 13
Registered: December 2012
Junior Member
The following might work.
Using the UNIQ function will give the indices, which you can use to get the value counts.
To get the counts for each value you'll then need to use the SHIFT function to offset the indices in order subtract from the original indices.

Assuming that a is already sorted, other wise use SORT(a) first
un = UNIQ(a)

In order to get the proper count for the first value, the unique array will need to be extended by 1 element.

un_extra = intarr(n_elements(un)+1)
un_extra[1:n_elements(un_extra)-1] = un

Add 1 to the array to account for the fact that indices start at 0
un_extra += 1
un_offset = SHIFT(un_extra, -1)

counts = un_offset - un_extra

Remove the last element in counts
counts = count[0:n_elements(counts)-2]
values = a[un]

Hope that helps ,if it works :)

Cheers
Josh
Re: Count duplicate elements in an array but keep their order! [message #85758 is a reply to message #85751] Tue, 03 September 2013 05:58 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Josh Sixsmith writes:

> The following might work.
> Using the UNIQ function will give the indices, which you can use to get the value counts.
> To get the counts for each value you'll then need to use the SHIFT function to offset the indices in order subtract from the original indices.

Using the UNIQ function on a floating point array is probably a bad
idea. You might find that ALL your floating point numbers are unique,
depending upon how you arrive at them.

I think to solve this problem you are going to have to convert the
floats to integers of whatever degree of "floating" accuracy makes sense
for the problem. Then work with the integers to find, count, and remove
duplicates.

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
Re: Count duplicate elements in an array but keep their order! [message #85885 is a reply to message #85758] Mon, 16 September 2013 10:51 Go to previous message
Steve Daal is currently offline  Steve Daal
Messages: 13
Registered: October 2011
Junior Member
On Tuesday, September 3, 2013 7:58:33 AM UTC-5, David Fanning wrote:
> Josh Sixsmith writes:
>
>
>
>> The following might work.
>
>> Using the UNIQ function will give the indices, which you can use to get the value counts.
>
>> To get the counts for each value you'll then need to use the SHIFT function to offset the indices in order subtract from the original indices.
>
>
>
> Using the UNIQ function on a floating point array is probably a bad
>
> idea. You might find that ALL your floating point numbers are unique,
>
> depending upon how you arrive at them.
>
>
>
> I think to solve this problem you are going to have to convert the
>
> floats to integers of whatever degree of "floating" accuracy makes sense
>
> for the problem. Then work with the integers to find, count, and remove
>
> duplicates.
>
>
>
> Cheers,
>
>
>
> David
>
> --
>
> David Fanning, Ph.D.
>
> Fanning Software Consulting, Inc.
>
> Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
>
> Sepore ma de ni thue. ("Perhaps thou speakest truth.")

Thanks Josh and David. That helped a lot.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Evenly timed events in a widget program
Next Topic: Texture image on a 3d curve: cgsurface and xobjview problems

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

Current Time: Wed Oct 08 13:34:19 PDT 2025

Total time taken to generate the page: 0.00598 seconds