Re: array 'minus' [message #36085] |
Tue, 19 August 2003 01:01 |
R.Bauer
Messages: 1424 Registered: November 1998
|
Senior Member |
|
|
tomson wrote:
> Hi, I'd like to eliminate some elements in a array. For example,
> A=[1,2,3,4,5,6,7,8,9,0]
> N=[3,6]
> I want to get a array b equal to A but without A(3) and A(6). How to do
> that?
>
> Thank you.
>
> Tomson
Dear Tomson
print,a_not_b(a,n)
% Compiled module: A_NOT_B.
% Compiled module: UNIQ.
0 1 2 4 5 7 8 9
You can get a_not_b and several others from our library.
http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_source/idl _html/dbase/a_not_b_dbase.pro.html
There is an a_and_b routine and some others available.
Some people asked how to unpack tgz files. So here is an explaination what
it is. The tgz extension is named tarball and it is the same as tar.gz.
You can unpack it on MS windows by winzip or on unix by gnu tar with tar
zxvf file.tgz.
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 'minus' [message #36086 is a reply to message #36085] |
Mon, 18 August 2003 23:13  |
tianyf_cn
Messages: 19 Registered: November 2002
|
Junior Member |
|
|
There are a few ways to do this.
1).Replace A(N) with -1 and then use Where() function to find
those elements that are not equal to -1.
A(N)=-1
b=A(where(A ne -1))
2). A more generic method to delete specified element(s) of an array.
Here we need some user-supplied function - del_ind. The function is given below.
b=del_ind(A,N)
***
;+
; Name:
; del_ind
;
; Purpose:
; To delete specified elements of an array.
;
;+++
;-
function del_ind, array, toDel
tmp=lonarr(n_elements(array))
tmp(todel)=-1
return,array(where(tmp ne -1))
end
;///
Tian.
"tomson" <tom2959@21cn.com> wrote in message news:<bhs3ot$1p1f$1@mail.cn99.com>...
> Hi, I'd like to eliminate some elements in a array. For example,
> A=[1,2,3,4,5,6,7,8,9,0]
> N=[3,6]
> I want to get a array b equal to A but without A(3) and A(6). How to do
> that?
>
> Thank you.
>
> Tomson
|
|
|
Re: array 'minus' [message #36087 is a reply to message #36086] |
Mon, 18 August 2003 22:12  |
Mark Hadfield
Messages: 783 Registered: May 1995
|
Senior Member |
|
|
Mark Hadfield wrote:
> tomson wrote:
>
>> Hi, I'd like to eliminate some elements in a array. For example,
>> A=[1,2,3,4,5,6,7,8,9,0]
>> N=[3,6]
>> I want to get a array b equal to A but without A(3) and A(6). How to do
>> that?
>
> Well, you could do this:
>
> B = make_array(size(A, /DIMENSIONS), VALUE=1B)
> B[N] = 0B
>
> A = A[where(B)]
Sorry, I didn't read your post very carefully and so gave an answer that
might confuse you. I used "B" for the mask array and saved the trimmed
values back in "A". You wanted the trimmed values in "B". The following
does this. Also it uses the "temporary" finction to delete the mask
array once it's no longer needed.
mask = make_array(size(A, /DIMENSIONS), VALUE=1B)
mask[N] = 0B
B = A[where(temporary(mask))]
--
Mark Hadfield "Ka puwaha te tai nei, Hoea tatou"
m.hadfield@niwa.co.nz
National Institute for Water and Atmospheric Research (NIWA)
|
|
|
Re: array 'minus' [message #36088 is a reply to message #36087] |
Mon, 18 August 2003 22:09  |
Mark Hadfield
Messages: 783 Registered: May 1995
|
Senior Member |
|
|
tomson wrote:
> Hi, I'd like to eliminate some elements in a array. For example,
> A=[1,2,3,4,5,6,7,8,9,0]
> N=[3,6]
> I want to get a array b equal to A but without A(3) and A(6). How to do
> that?
Well, you could do this:
B = make_array(size(A, /DIMENSIONS), VALUE=1B)
B[N] = 0B
A = A[where(B)]
The first two statements construct a mask array, B, with the same
dimensions as A. The elements of B are set equal to 1 (true) when we
wish to retain the corresponding element of A and 0 (false) otherwise.
The final statement generates a 1D array containing only those elements
of A that are flagged in B as true. If there are no true elements in B
it will generate an error.
Also I suggest you specify
compile_opt STRICTARRSUBS
at the beginning of your routine or code block (as long as you are using
a version that supports it: 5.6 or greater, I think). This will ensure
that subscripting B with out-of-bounds values on N will raise an error.
--
Mark Hadfield "Ka puwaha te tai nei, Hoea tatou"
m.hadfield@niwa.co.nz
National Institute for Water and Atmospheric Research (NIWA)
|
|
|
Re: array 'minus' [message #36089 is a reply to message #36088] |
Mon, 18 August 2003 20:13  |
mperrin+news
Messages: 81 Registered: May 2001
|
Member |
|
|
tomson <tom2959@21cn.com> wrote:
> Hi, I'd like to eliminate some elements in a array. For example,
> A=[1,2,3,4,5,6,7,8,9,0]
> N=[3,6]
> I want to get a array b equal to A but without A(3) and A(6). How to do
> that?
Well, you can do something like this, using Craig Markwardt's CMSET_OP.PRO:
index_a = indgen(n_elements(a))
index_minus = cmset_op(index_a, "and",/not2,N)
b = a[index_minus]
Just bear in mind there are probably more memory-efficient ways of doing this,
if you're dealing with huge arrays. For anything under a few hundred thousand
elements, the above should work just fine.
- Marshall
|
|
|