Re: Finding duplicate values in an array [message #2582] |
Tue, 19 July 1994 12:12 |
andy
Messages: 31 Registered: November 1993
|
Member |
|
|
In article <30goug$bkp@nyx10.cs.du.edu>, slohmeie@nyx10.cs.du.edu (stephen lohmeier) writes:
> In article <1994Jul18.171646@chapman.gsfc.nasa.gov>,
> Ian Sprod <sprod@chapman.gsfc.nasa.gov> wrote:
>>
>> I seem to remember the topic of finding duplicate values in an array
>> was discussed some months ago, but i never did read the end of the thread.
>>
>> What is the _fastest_ way to throw duplicated values out?
>>
>> thanks
>>
>> ian
>
> This is how I'd do it:
>
> ;array of values
> arr = [.14, .11, .13, .12, .13, .13, .12, .13, .09, .13, .10, .12, .09, .14]
>
> ;sort array
> arr_sorted = arr(sort(arr))
>
> ;find disparity between adjacent elements
> n = n_elements(arr_sorted)
> disparity = arr_sorted(1:n-1)-arr_sorted(0:n-2)
>
> ;find locations where disparities are non-zero
> index = where(disparity)+1
>
> ;non-duplicated array
> arr_nodups = [arr_sorted(0), arr_sorted(index)]
>
> END
>
> Hope this helps.
>
>
How about this...
arr = [.14, .11, .13, .12, .13, .13, .12, .13, .09, .13, .10, .12, .09, .14]
arr_nodups = arr(uniq(arr,sort(arr)))
Andy
--
,__o Andrew F. Loughe (Mail Code 971)
-\_<, NASA Goddard Space Flight Center phone: (301) 286-5899
(*)/'(*) Greenbelt, MD 20771 email: andy.loughe@gsfc.nasa.gov
|
|
|
Re: Finding duplicate values in an array [message #2585 is a reply to message #2582] |
Tue, 19 July 1994 07:45  |
slohmeie
Messages: 2 Registered: July 1993
|
Junior Member |
|
|
In article <1994Jul18.171646@chapman.gsfc.nasa.gov>,
Ian Sprod <sprod@chapman.gsfc.nasa.gov> wrote:
>
> I seem to remember the topic of finding duplicate values in an array
> was discussed some months ago, but i never did read the end of the thread.
>
> What is the _fastest_ way to throw duplicated values out?
>
> thanks
>
> ian
This is how I'd do it:
;array of values
arr = [.14, .11, .13, .12, .13, .13, .12, .13, .09, .13, .10, .12, .09, .14]
;sort array
arr_sorted = arr(sort(arr))
;find disparity between adjacent elements
n = n_elements(arr_sorted)
disparity = arr_sorted(1:n-1)-arr_sorted(0:n-2)
;find locations where disparities are non-zero
index = where(disparity)+1
;non-duplicated array
arr_nodups = [arr_sorted(0), arr_sorted(index)]
END
Hope this helps.
|
|
|