help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Inline-If ?


From: Stefan Neumann
Subject: Re: Inline-If ?
Date: Fri, 14 May 2010 20:43:25 +0200

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 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" ?

THX
Stefan


reply via email to

[Prev in Thread] Current Thread [Next in Thread]