[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Inline-If ?
From: |
Jaroslav Hajek |
Subject: |
Re: Inline-If ? |
Date: |
Fri, 14 May 2010 21:16:46 +0200 |
On Fri, May 14, 2010 at 8:43 PM, Stefan Neumann <address@hidden> wrote:
> 2010/5/14 Jaroslav Hajek <address@hidden>
>>
>> On Fri, May 14, 2010 at 3:19 PM, Stefan Neumann <address@hidden> wrote:
>> > is there something like an "inline-if" or "immediate if" in octave?
>>
>> In Octave 3.3.51+, there is merge() or ifelse()
>
>
> Jaroslav, thanks for the info. With that I found the answer.
>
> OK, so either go to the test-version or take care of it some other way :-)
>
> % --- file: ifelse.m --- %
> function RES = ifelse(c,t,f)
> RES = c .* t + (1-c) .* f ;
> end
>
> This seems to do it. Testing:
>
> octave:10> a = round(rand(1,10))
> a = 0 0 0 1 1 1 1 0 1 0
> octave:11> ifelse(a == 1, 2, 3)
> ans = 3 3 3 2 2 2 2 3 2 3
>
It's close enough for practical work. But if NaNs and Infs are
present, it won't do the trick.
>
>>
>> ... It works for arrays, too, but it doesn't short-circuit (functions
>> can't).
>
> Didn't get that part. Probably something very basic. What did you mean by
> "doesn't short-circuit" ?
>
both args are always evaluated. See
http://en.wikipedia.org/wiki/Short-circuit_evaluation
--
RNDr. Jaroslav Hajek, PhD
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz