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

Home » Public Forums » archive » make all array values equal to zero
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
make all array values equal to zero [message #89857] Thu, 11 December 2014 07:12 Go to next message
g.nacarts is currently offline  g.nacarts
Messages: 148
Registered: November 2013
Senior Member
Hi

Can I use the "greater than" operator to make all array values greater than 100 equal to zero?

E.g. A = [[1,2,3],[120,4,200]] to become A = [[1,2,3],[0,4,0]]

Many Thanks
Re: make all array values equal to zero [message #89858 is a reply to message #89857] Thu, 11 December 2014 07:20 Go to previous messageGo to next message
lecacheux.alain is currently offline  lecacheux.alain
Messages: 325
Registered: January 2008
Senior Member
On Thursday, December 11, 2014 4:12:53 PM UTC+1, g.na...@gmail.com wrote:
> Hi
>
> Can I use the "greater than" operator to make all array values greater than 100 equal to zero?
>
> E.g. A = [[1,2,3],[120,4,200]] to become A = [[1,2,3],[0,4,0]]
>
> Many Thanks

The IDL way would be:
A[where(A gt 100, /NULL)] = 0

alx.
Re: make all array values equal to zero [message #89859 is a reply to message #89857] Thu, 11 December 2014 07:20 Go to previous messageGo to next message
Helder Marchetto is currently offline  Helder Marchetto
Messages: 520
Registered: November 2011
Senior Member
On Thursday, December 11, 2014 4:12:53 PM UTC+1, g.na...@gmail.com wrote:
> Hi
>
> Can I use the "greater than" operator to make all array values greater than 100 equal to zero?
>
> E.g. A = [[1,2,3],[120,4,200]] to become A = [[1,2,3],[0,4,0]]
>
> Many Thanks

Hi,
not with gt operator. You can use the where() function:

pos = where(a gt 200, cnt)
if cnt gt 0 then a[pos] = 0

Cheers,
Helder
Re: make all array values equal to zero [message #89861 is a reply to message #89857] Thu, 11 December 2014 07:58 Go to previous messageGo to next message
Michael Galloy is currently offline  Michael Galloy
Messages: 1114
Registered: April 2006
Senior Member
On 12/11/14, 8:12 am, g.nacarts@gmail.com wrote:
> Hi
>
> Can I use the "greater than" operator to make all array values
> greater than 100 equal to zero?
>
> E.g. A = [[1,2,3],[120,4,200]] to become A = [[1,2,3],[0,4,0]]
>
> Many Thanks
>

In addition to the other methods mentioned:

IDL> print, (A le 100) * A
1 2 3
0 4 0

And, if you really want to know if there was some way to do it with
greater than operator, yes you can:

IDL> print, (1B - (A gt 100)) * A
1 2 3
0 4 0

Mike
--
Michael Galloy
www.michaelgalloy.com
Modern IDL: A Guide to IDL Programming (http://modernidl.idldev.com)
Research Mathematician
Tech-X Corporation
Re: make all array values equal to zero [message #89862 is a reply to message #89861] Thu, 11 December 2014 08:59 Go to previous messageGo to next message
lecacheux.alain is currently offline  lecacheux.alain
Messages: 325
Registered: January 2008
Senior Member
On Thursday, December 11, 2014 4:58:37 PM UTC+1, Mike Galloy wrote:
> On 12/11/14, 8:12 am, g.nacarts@gmail.com wrote:
>> Hi
>>
>> Can I use the "greater than" operator to make all array values
>> greater than 100 equal to zero?
>>
>> E.g. A = [[1,2,3],[120,4,200]] to become A = [[1,2,3],[0,4,0]]
>>
>> Many Thanks
>>
>
> In addition to the other methods mentioned:
>
> IDL> print, (A le 100) * A
> 1 2 3
> 0 4 0
>
> And, if you really want to know if there was some way to do it with
> greater than operator, yes you can:
>
> IDL> print, (1B - (A gt 100)) * A
> 1 2 3
> 0 4 0
>
> Mike
> --
> Michael Galloy
> www.michaelgalloy.com
> Modern IDL: A Guide to IDL Programming (http://modernidl.idldev.com)
> Research Mathematician
> Tech-X Corporation

IDL> A=randomn(rien,10000000)
IDL> tic & A *= (A gt 1) & toc
% Time elapsed: 0.078000069 seconds.
IDL> tic & A[where(A gt 1,/NULL)]=0 & toc
% Time elapsed: 0.047000170 seconds.

The "where" solution appears to be somewhat faster.

alx.
Re: make all array values equal to zero [message #89863 is a reply to message #89862] Thu, 11 December 2014 09:52 Go to previous messageGo to next message
Michael Galloy is currently offline  Michael Galloy
Messages: 1114
Registered: April 2006
Senior Member
On 12/11/14, 9:59 AM, alx wrote:
> IDL> A=randomn(rien,10000000)
> IDL> tic & A *= (A gt 1) & toc
> % Time elapsed: 0.078000069 seconds.
> IDL> tic & A[where(A gt 1,/NULL)]=0 & toc
> % Time elapsed: 0.047000170 seconds.
>
> The "where" solution appears to be somewhat faster.

Your solution should scale with the number of elements which match the
condition.

So for 50% of the elements matching:

IDL> n = 10000000L
IDL> a = 2.0 * randomu(seed, n)
IDL> tic & a *= (A gt 1) & toc
Elapsed Time: 0.024550
IDL> a = 2.0 * randomu(seed, n)
IDL> tic & A[where(A gt 1,/NULL)]=0 & toc
Elapsed Time: 0.023005

For about 15% of the elements matching:

IDL> a = randomn(seed, n)
IDL> tic & A[where(A gt 1,/NULL)]=0 & toc
Elapsed Time: 0.014616

For all of the elements matching:

IDL> a = randomu(seed, n) + 1.0
IDL> tic & A[where(A gt 1,/NULL)]=0 & toc
Elapsed Time: 0.024564

But, in any case, the WHERE solution seems equal or faster in all cases.

Mike
--
Michael Galloy
www.michaelgalloy.com
Modern IDL: A Guide to IDL Programming (http://modernidl.idldev.com)
Research Mathematician
Tech-X Corporation
Re: make all array values equal to zero [message #89869 is a reply to message #89857] Fri, 12 December 2014 06:54 Go to previous message
g.nacarts is currently offline  g.nacarts
Messages: 148
Registered: November 2013
Senior Member
Thanks everyone for your response. All comments were very helpful :)
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: plot positional degrees
Next Topic: Rendering method software vs hardware

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

Current Time: Wed Oct 08 15:07:06 PDT 2025

Total time taken to generate the page: 0.00793 seconds