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)
|