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

Home » Public Forums » archive » Re: Array element deletion
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
Re: Array element deletion [message #36380] Sat, 13 September 2003 10:05
R.Bauer is currently offline  R.Bauer
Messages: 1424
Registered: November 1998
Senior Member
Edd Edmondson 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.
>

Just in addition to Davids answer.

a_not_b
http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_source/idl _html/dbase/a_not_b_dbase.pro.html

regards
Reimar


--
Forschungszentrum Juelich
email: R.Bauer@fz-juelich.de
http://www.fz-juelich.de/icg/icg-i/
============================================================ ======
a IDL library at ForschungsZentrum Juelich
http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_lib_intro. html
Re: Array element deletion [message #36385 is a reply to message #36380] Fri, 12 September 2003 06:44 Go to previous message
Edd Edmondson is currently offline  Edd Edmondson
Messages: 50
Registered: January 2003
Member
Excellent, thanks for the speedy response :-)

--
Edd
Re: Array element deletion [message #36386 is a reply to message #36385] Fri, 12 September 2003 06:31 Go to previous message
David Fanning is currently offline  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 Go to previous message
jjbezair is currently offline  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 Go to previous message
Edd Edmondson is currently offline  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
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Contour overlay
Next Topic: Re: Floating Point Slider Widgets

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

Current Time: Wed Oct 08 19:36:12 PDT 2025

Total time taken to generate the page: 0.00774 seconds