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

Home » Public Forums » archive » Re: replace array's elemets
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: replace array's elemets [message #35158] Sat, 17 May 2003 15:15
James Kuyper is currently offline  James Kuyper
Messages: 425
Registered: March 2000
Senior Member
Reimar Bauer wrote:
...
>>> Roberto wrote:
>>>
>>>> Hi
>>>> I have a 100x100 array.
>>>> How can I replace an element if is different from 0?
>>>> I would replace all elements different from 0 with 1.
>>>> thank and sorry for the trouble.
>>>> Roberto
...
> print,(abs(x)<1)

That works only as long as 'x' is an integer array, which may have been
the case. It won't do the right thing x is a floating point array
containing some values between 0.0 and 1.0. However, even for integer
arrays, I would expect "x ne 0" to be the faster solution.
Re: replace array's elemets [message #35165 is a reply to message #35158] Sat, 17 May 2003 04:13 Go to previous message
R.Bauer is currently offline  R.Bauer
Messages: 1424
Registered: November 1998
Senior Member
James Kuyper wrote:

> Reimar Bauer wrote:
>>
>> Roberto wrote:
>>
>>> Hi
>>> I have a 100x100 array.
>>> How can I replace an element if is different from 0?
>>> I would replace all elements different from 0 with 1.
>>> thank and sorry for the trouble.
>>> Roberto
>>
>> Dear Roberto
>>
>> I suggest something like this
>>
>> example data:
>> x=indgen(100,100)
>>
>> result:
>> result=x<1
>
> That doesn't do the right thing with negative numbers. "result=x ne 0"
> is the right choice. More generally,
>
> x[where(x ne 0)] = 1
>
> has the advantage of being more easily generalized to the case of more
> complicated search conditions, and different replacement values.
> However, it will produce an error message if no replacements are needed.


and what did you say to this:

print,(abs(x)<1)

I often use myself "where" :-)

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: replace array's elemets [message #35170 is a reply to message #35165] Fri, 16 May 2003 15:19 Go to previous message
James Kuyper is currently offline  James Kuyper
Messages: 425
Registered: March 2000
Senior Member
Reimar Bauer wrote:
>
> Roberto wrote:
>
>> Hi
>> I have a 100x100 array.
>> How can I replace an element if is different from 0?
>> I would replace all elements different from 0 with 1.
>> thank and sorry for the trouble.
>> Roberto
>
> Dear Roberto
>
> I suggest something like this
>
> example data:
> x=indgen(100,100)
>
> result:
> result=x<1

That doesn't do the right thing with negative numbers. "result=x ne 0"
is the right choice. More generally,

x[where(x ne 0)] = 1

has the advantage of being more easily generalized to the case of more
complicated search conditions, and different replacement values.
However, it will produce an error message if no replacements are needed.
Re: replace array's elemets [message #35171 is a reply to message #35170] Fri, 16 May 2003 13:45 Go to previous message
R.Bauer is currently offline  R.Bauer
Messages: 1424
Registered: November 1998
Senior Member
Roberto wrote:

> Hi
> I have a 100x100 array.
> How can I replace an element if is different from 0?
> I would replace all elements different from 0 with 1.
> thank and sorry for the trouble.
> Roberto

Dear Roberto

I suggest something like this

example data:
x=indgen(100,100)

result:
result=x<1


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: replace array's elemets [message #35176 is a reply to message #35171] Fri, 16 May 2003 12:04 Go to previous message
Liam E. Gumley is currently offline  Liam E. Gumley
Messages: 378
Registered: January 2000
Senior Member
"Roberto" <graftons@tiscalinet.it> wrote in message
news:4ac6b3e5.0305160733.50173373@posting.google.com...
> I have a 100x100 array.
> How can I replace an element if is different from 0?
> I would replace all elements different from 0 with 1.
> thank and sorry for the trouble.

IDL> a = [0, 1, 2, 3, 0, 5, 6, 7, 0, 9]
IDL> a[*] = a ne 0
IDL> print, a, format='(10i4)'
0 1 1 1 0 1 1 1 0 1

If your array contains floating point values, you should use something like

IDL> a = [0.0, 1, 2, 3, 0, 5, 6, 7, 0, 9]
IDL> info = machar()
IDL> value = 0.0
IDL> a[*] = abs(a - value) gt info.eps
IDL> print, a, format='(10f4.1)'
0.0 1.0 1.0 1.0 0.0 1.0 1.0 1.0 0.0 1.0

Cheers,
Liam.
Practical IDL Programming
http://www.gumley.com/
Re: replace array's elemets [message #35188 is a reply to message #35176] Fri, 16 May 2003 09:13 Go to previous message
Chris[1] is currently offline  Chris[1]
Messages: 23
Registered: January 2003
Junior Member
Hi Roberto;

Use the "where" function - as in

(assume x is your 100x100 array)

w = where(x ne 0, count)
if count gt 0 then x[w] = 1


Cheers;

Chris

"Roberto" <graftons@tiscalinet.it> wrote in message
news:4ac6b3e5.0305160733.50173373@posting.google.com...
> Hi
> I have a 100x100 array.
> How can I replace an element if is different from 0?
> I would replace all elements different from 0 with 1.
> thank and sorry for the trouble.
> Roberto
Re: replace array's elemets [message #35189 is a reply to message #35188] Fri, 16 May 2003 09:00 Go to previous message
btt is currently offline  btt
Messages: 345
Registered: December 2000
Senior Member
Roberto wrote:
> Hi
> I have a 100x100 array.
> How can I replace an element if is different from 0?
> I would replace all elements different from 0 with 1.
> thank and sorry for the trouble.
> Roberto

Hi Roberto,

You can use the WHERE function to get the indices of the elements that
match your value (in your case 0). You do need to be a bit careful if
your array is floating decimal type - in that case you must think about
machine precision issues. Search on Google for more info about this.


indices = where(myArray EQ someValue, count)

if count NE 0 then myArray[indices] = 1


If you are new to IDL, I encourage you get one of the very good
introductory manuals that are available. Your won't reget it.

Liam Gumley's www.gumley.com
David Fanning's www.dfanning.com
Ronn Kling's www.rlkling.com

Cheers,
Ben
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: plotting from high values to low values
Next Topic: read pixels

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

Current Time: Wed Oct 08 15:15:50 PDT 2025

Total time taken to generate the page: 0.00731 seconds