Re: "sign" function

From: Kyle Griffin <ksgriffin2_at_nyahnyahspammersnyahnyah>
Date: Sun Apr 06 2014 - 13:10:32 MDT

Hi Verena,

Have you tried Dennis' very nice implementation of the sign function? It
should work with an array of any dimension (including three and four
dimensions). If it doesn't work, can you post some sample code showing how
you're calling it and what the error messages are?

Kyle

----------------------------------------
Kyle S. Griffin
Department of Atmospheric and Oceanic Sciences
University of Wisconsin - Madison
Room 1421
1225 W Dayton St, Madison, WI 53706
Email: ksgriffin2@wisc.edu

On Sun, Apr 6, 2014 at 12:02 PM, Verena Lili <verena.prick@gmail.com> wrote:

> Thanks Denni, Brian and Kyle,
>
> Anyway, still wondering to implement this function for multi-dimensional
> arrays.. eg.
>
> X= [time, level, lat] ---> Y= (X^2) * sign (X) , will result Y[time,
> level, lat]
>
> Any suggestion would be really appreciated.
>
> Thanks
>
>
> On Mon, Apr 7, 2014 at 3:38 AM, Dennis Shea <shea@ucar.edu> wrote:
>
>> Untested
>>
>> undef("sign_matlab")
>> function sign_matlab( X:numeric )
>> ;
>> ; Mimic Matlab 'sign' function
>>
>> ; sign(X) returns the sign of each element of 'X'
>> ; 3 possible return values for non _FillValue elements: -1, 0, 1
>> ; The return value type will be the same type as 'X'
>> ;
>> local typex, zero, onePos, oneNeg, result
>> begin
>> typex = typeof(X)
>>
>> zero = totype( 0, typex)
>> onePos = totype( 1, typex)
>> oneNeg = totype(-1, typex)
>>
>> result = X ; same size, shape and type as 'X'
>>
>> result = where(X.eq.zero, zero , result)
>> result = where(X.gt.zero, onePos, result)
>> result = where(X.lt.zerp, oneNeg, result)
>>
>> return(result)
>> end
>>
>> ~
>>
>>
>> On 4/6/14, 12:11 PM, Verena Lili wrote:
>>
>>> This is not what I intended to do..
>>>
>>> let's say I have a simple array X =(/-5,-2,-1,5,2,1/)
>>>
>>> thus Y= X^2 * sign (X)
>>>
>>> will produce: Y=(/-25,-4,-1,25,2,1/)
>>>
>>> or M= sqrt (Y) * sign (Y)
>>>
>>> would be M = (/-5,-2,-1,5,2,1/)
>>>
>>> The main things are that I need to retain the + or - sign in the values.
>>> I
>>> need this function for my complex number analyses. Then another question
>>> arising, how if my arrays contain more than one dimension let' say 3D.
>>> I'was wondering befored whether a similar function called as "sign"
>>> which
>>> is availabel in Matlab is available in NCL.
>>>
>>> Thanks
>>>
>>>
>>> On Mon, Apr 7, 2014 at 2:52 AM, Kyle Griffin <ksgriffin2@wisc.edu>
>>> wrote:
>>>
>>> You might try something like this...
>>>>
>>>> Given that x = (/time, level, lat, lon/) as you specified in your email
>>>> -
>>>> or honestly, any dimension should work fine
>>>>
>>>> signx = where(x.eq.0,x@_FillValue,x)
>>>> signx = x/abs(x)
>>>> signx = where(ismissing(signx),0,x)
>>>> signx = where(ismissing(x),x@_FillValue,signx)
>>>>
>>>> The middle line alone would work if you knew none of your values were
>>>> exactly zero, but would cause divide-by-zero errors if there were zero
>>>> values. Since I believe the where function evaluates the entirety of
>>>> both
>>>> the 'true' and 'false' arrays, you would run into the same problem with
>>>> doing a x/abs(x) in the where function if you don't set all the zero
>>>> values
>>>> to missing first. This also has a problem, though, in that it will end
>>>> up
>>>> setting all missing values to '0' sign in the third line, but we can fix
>>>> that with the fourth line and simply copy the missing value locations
>>>> out
>>>> of the original data.
>>>>
>>>> I haven't tested these lines of code, but I think they should get the
>>>> job
>>>> done.
>>>>
>>>> Let the list know how this works out!
>>>>
>>>>
>>>> Kyle
>>>>
>>>> ----------------------------------------
>>>> Kyle S. Griffin
>>>> Department of Atmospheric and Oceanic Sciences
>>>> University of Wisconsin - Madison
>>>> Room 1421
>>>> 1225 W Dayton St, Madison, WI 53706
>>>> Email: ksgriffin2@wisc.edu
>>>>
>>>>
>>>> On Sun, Apr 6, 2014 at 9:59 AM, Verena Lili <verena.prick@gmail.com
>>>> >wrote:
>>>>
>>>> The problem is following:
>>>>>
>>>>>
>>>>> ncl 0>
>>>>> ncl 1> function sign( X )
>>>>> ncl 2> begin
>>>>> ncl 3> if X.eq.0 then
>>>>> ncl 4> result = 0
>>>>> ncl 5> else
>>>>> ncl 6> if X.gt.0 then
>>>>> ncl 7> result = 1
>>>>> ncl 8> else
>>>>> ncl 9> result = -1
>>>>> ncl 10> end if
>>>>> ncl 11> end if
>>>>> ncl 12> return( result )
>>>>> ncl 13> end
>>>>> ncl 14>
>>>>> ncl 15> a=(/-2,-3,-4,4,5,6-1/)
>>>>> ncl 16>
>>>>> ncl 17>* b=(a^2)*sign(a)*
>>>>> *fatal:Conditional statements (if and do while) require SCALAR logical
>>>>> values, see all and any functions*
>>>>> *fatal:Execute: Error occurred at or near line 11*
>>>>>
>>>>> *fatal:Execute: Error occurred at or near line 17*
>>>>>
>>>>>
>>>>> Any subroutine that can work as global "sign" function in ncl and can
>>>>> be
>>>>> used for multidimensional arrays? e.g.
>>>>>
>>>>> X= [time, level, lat, lon]
>>>>>
>>>>> y= (X^2) * sign (X)
>>>>>
>>>>> Thank you.
>>>>>
>>>>>
>>>>>
>>>>> On Thu, Apr 3, 2014 at 3:30 AM, Verena Lili <verena.prick@gmail.com
>>>>> >wrote:
>>>>>
>>>>> Thanks Brian!
>>>>>>
>>>>>>
>>>>>> On Wed, Apr 2, 2014 at 6:50 PM, Brian Medeiros <brianpm@ucar.edu>
>>>>>> wrote:
>>>>>>
>>>>>> You could write that function:
>>>>>>>
>>>>>>> function sign( X )
>>>>>>> begin
>>>>>>> if X.eq.0 then
>>>>>>> result = 0
>>>>>>> else
>>>>>>> if X.gt.0 then
>>>>>>> result = 1
>>>>>>> else
>>>>>>> result = -1
>>>>>>> end if
>>>>>>> end if
>>>>>>> return( result )
>>>>>>> end
>>>>>>>
>>>>>>> .brian
>>>>>>>
>>>>>>>
>>>>>>> On Apr 2, 2014, at 10:40 AM, Verena Lili <verena.prick@gmail.com>
>>>>>>> wrote:
>>>>>>>
>>>>>>> Hi NCL,
>>>>>>>
>>>>>>> Is there any function in NCL similar like `"*sign" *function in
>>>>>>> Matlab?
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>> Regards,
>>>>>>> Dr. Verena.
>>>>>>> School of Marine and Atmospheric Sciences
>>>>>>> Stony Brook University
>>>>>>> homepage: http://www.somas.stonybrook.edu/
>>>>>>> _______________________________________________
>>>>>>> ncl-talk mailing list
>>>>>>> List instructions, subscriber options, unsubscribe:
>>>>>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>> --
>>>>>> Regards,
>>>>>> Dr. Verena.
>>>>>> School of Marine and Atmospheric Sciences
>>>>>> Stony Brook University
>>>>>> homepage: http://www.somas.stonybrook.edu/
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Regards,
>>>>> Dr. Verena.
>>>>> School of Marine and Atmospheric Sciences
>>>>> Stony Brook University
>>>>> homepage: http://www.somas.stonybrook.edu/
>>>>>
>>>>> _______________________________________________
>>>>> ncl-talk mailing list
>>>>> List instructions, subscriber options, unsubscribe:
>>>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>>>>
>>>>>
>>>>>
>>>>
>>>
>>>
>>>
>>> _______________________________________________
>>> ncl-talk mailing list
>>> List instructions, subscriber options, unsubscribe:
>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>>
>>>
>
>
> --
> Regards,
> Dr. Verena.
> School of Marine and Atmospheric Sciences
> Stony Brook University
> homepage: http://www.somas.stonybrook.edu/
>
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Sun Apr 06 13:11:06 2014

This archive was generated by hypermail 2.1.8 : Tue Apr 15 2014 - 10:45:19 MDT