Re: Ternary Operator for Arrays [message #39531] |
Tue, 25 May 2004 14:00  |
JD Smith
Messages: 850 Registered: December 1999
|
Senior Member |
|
|
On Tue, 25 May 2004 12:07:18 -0700, Jack Yin wrote:
> Does anyone know of a good clean way of doing ternary operation on
> arrays without using FOR loops and nested IF statements?
>
> For example, I have three arrays A, B and C and I want to do A = B /
> C, but there are elements in C that are zeros and I want to set the
> corresponding elements in A to zeros without actually doing the
> division to avoid divide by zero error. The ternary operator (?:)
> only works on scalers. Is there something comparable for arrays?
>
> Any help would be greatly appreciated!
A couple of ways, not necessarily clean:
For integers or floats:
a=b/(c eq 0 + c*(d=c ne 0))*d
For positive floats:
a=b/(c>(machar()).xmin)*c ne 0.0
JD
|
|
|
Re: Ternary Operator for Arrays [message #39606 is a reply to message #39531] |
Wed, 26 May 2004 08:32  |
jack.c.yin
Messages: 5 Registered: January 2004
|
Junior Member |
|
|
JD Smith <jdsmith@as.arizona.edu> wrote in message news:<pan.2004.05.25.21.00.07.321890@as.arizona.edu>...
> On Tue, 25 May 2004 12:07:18 -0700, Jack Yin wrote:
>
>> Does anyone know of a good clean way of doing ternary operation on
>> arrays without using FOR loops and nested IF statements?
>>
>> For example, I have three arrays A, B and C and I want to do A = B /
>> C, but there are elements in C that are zeros and I want to set the
>> corresponding elements in A to zeros without actually doing the
>> division to avoid divide by zero error. The ternary operator (?:)
>> only works on scalers. Is there something comparable for arrays?
>>
>> Any help would be greatly appreciated!
>
> A couple of ways, not necessarily clean:
>
> For integers or floats:
>
> a=b/(c eq 0 + c*(d=c ne 0))*d
>
> For positive floats:
>
> a=b/(c>(machar()).xmin)*c ne 0.0
>
>
> JD
JD,
Thanks for the tips. They will work for me. Really appreciate the help!
Jack
|
|
|