Count duplicate elements in an array but keep their order! [message #85750] |
Fri, 30 August 2013 16:23  |
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 #85885 is a reply to message #85758] |
Mon, 16 September 2013 10:51  |
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.
|
|
|