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

Home » Public Forums » archive » Removing specific elements in an array
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: Removing specific elements in an array [message #94532 is a reply to message #94531] Tue, 27 June 2017 14:32 Go to previous messageGo to previous message
Michael Galloy is currently offline  Michael Galloy
Messages: 1114
Registered: April 2006
Senior Member
On 6/27/17 2:17 PM, thtran296@gmail.com wrote:
> Hello everybody,
> So I have an array that looks like this:
> x = [4,5,17,18,30,38,50,51,70]
>
> The goal is to get rid of the element that is only 1 unit larger than the previous one.
> The ideal output should look like:
> x = [4,17,30,38,50,70] (since 5,18, and 51 are deleted)
>
> I have tried all sorts of loops but it is not working. Here's my own attempt.
> y = [4,5,8,15,30,31,45,70]
> y2 = [y,0]
> for i = 0,6 do begin
> if y2(i) ne 0 then begin
> if y2(i+1) - y2(i) le 1 then begin
> remove, i, y
> endif
> endif
> endfor
>
> Please help. I appreciate it.
>

How about this?

IDL> x = [4, 5, 17, 18, 30, 38, 50, 51, 70]
IDL> remove_ind = where(x[1:*] - x[0:-2] eq 1, count) + 1L
IDL> keep_ind = mg_complement(remove_ind, n_elements(x))
IDL> print, remove_ind
1 3 7
IDL> print, keep_ind
0 2 4 5 6 8
IDL> print, x[remove_ind]
5 18 51
IDL> print, x[keep_ind]
4 17 30 38 50 70

MG_COMPLEMENT is available here:


https://github.com/mgalloy/mglib/blob/master/src/indices/mg_ complement.pro

But it is fairly short:

function mg_complement, indices, n, count=ncomplement
compile_opt strictarr, strictarrsubs

all = bytarr(n)
valid_indices = where(indices ge 0 and indices lt n, n_valid)
if (n_valid gt 0L) then all[indices[valid_indices]] = 1B
return, where(all eq 0B, ncomplement)
end

Mike
--
Michael Galloy
www.michaelgalloy.com
Modern IDL: A Guide to IDL Programming (http://modernidl.idldev.com)
[Message index]
 
Read Message
Read Message
Read Message
Previous Topic: Re: Linux installation problems..
Next Topic: Check numerical derivatives

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

Current Time: Wed Oct 08 13:35:41 PDT 2025

Total time taken to generate the page: 0.00429 seconds