make all array values equal to zero [message #89857] |
Thu, 11 December 2014 07:12  |
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   |
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   |
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   |
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   |
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   |
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
|
|
|
|