|
|
Re: Array element deletion [message #36386 is a reply to message #36385] |
Fri, 12 September 2003 06:31  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Edd Edmondson writes:
> Supposing I have a big array and I also have an array containing indices
> of unwanted elements. Is there a neat way of removing those elements from
> the array?
>
> Attempts using WHERE() have failed for me* and currently I'm looping
> through the array of indices and deleting them one at a time - which is
> fine when I've only got 20 or 30 of them but I doubt it'll scale well.
Here is a function from my web page:
FUNCTION SetDifference, a, b
; = a and (not b) = elements in A but not in B
mina = Min(a, Max=maxa)
minb = Min(b, Max=maxb)
IF (minb GT maxa) OR (maxb LT mina) THEN RETURN, a ;No intersection...
r = Where((Histogram(a, Min=mina, Max=maxa) NE 0) AND $
(Histogram(b, Min=mina, Max=maxa) EQ 0), count)
IF count eq 0 THEN RETURN, -1 ELSE RETURN, r + mina
END
Here is how it works. Suppose you have an array:
array = [3.5, 4.8, 9.3, 2.1, 7.6, 4.6]
And an array of indices you don't want:
bad = [2, 4]
You would do this:
possible = Indgen(N_Elements(array))
good = SetDifference(possible, bad)
Print, possible
Print, good
newArray = array[good]
Print, newArray
You can learn more about these set methods here:
http://www.dfanning.com/tips/set_operations.html
Cheers,
David
--
David W. Fanning, Ph.D.
Fanning Software Consulting, Inc.
Phone: 970-221-0438, E-mail: david@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|
|
|
Re: Array element deletion [message #36387 is a reply to message #36386] |
Fri, 12 September 2003 06:27  |
jjbezair
Messages: 2 Registered: July 2003
|
Junior Member |
|
|
In article <bjsh0u$q77$1@news.ox.ac.uk>,
Edd Edmondson <eddedmondson@hotmail.com> wrote:
> Supposing I have a big array and I also have an array containing indices
> of unwanted elements. Is there a neat way of removing those elements from
> the array?
>
> Attempts using WHERE() have failed for me* and currently I'm looping
> through the array of indices and deleting them one at a time - which is
> fine when I've only got 20 or 30 of them but I doubt it'll scale well.
>
>
> * - I tried to make a list of indices that I wanted to keep using
> WHERE(indgen(x) ne unwanted) but that only produces the first
> n_elements(unwanted) of the correct indices.
>
> --
> Edd
I'm sure one or more of the gurus here will have a faster solution that
uses histogram, but I've always used index masks for this sort of thing
indexMask = lonarr(n_elements(bigArray))
indexMask(unwanted) = 1
wanted = where(indexMask == 0)
smallArray = bigArray(wanted)
of course you should check that where returns a nonzero array, etc., and
this method uses 4 times the memory of the original array, but memory is
cheap these days, and these 4 lines of code are quick to type...
I'm curious to see what more efficient techniques other people here use.
regards,
Jeff
--
|
|
|
Re: Array element deletion [message #36388 is a reply to message #36387] |
Fri, 12 September 2003 06:19  |
Edd Edmondson
Messages: 50 Registered: January 2003
|
Member |
|
|
Edd Edmondson <eddedmondson@hotmail.com> wrote:
> Attempts using WHERE() have failed for me* and currently I'm looping
> through the array of indices and deleting them one at a time - which is
> fine when I've only got 20 or 30 of them but I doubt it'll scale well.
... and indeed that method is broken, since everytime I delete an element
the indices of the unwanted ones change....
--
Edd
|
|
|