help-octave
[Top][All Lists]

## Re: Stupid New User Question

 From: Geordie McBain Subject: Re: Stupid New User Question Date: Wed, 12 Jul 2006 10:11:51 +1000

```Hello.  The trick here is in the value of (retval<0) .  If retval is an
array, (retval<0) is an array of the same shape with true/false
elements, so it's not really a suitable thing to use in the condition of
an if statement.  I attach two implementations of killnegatives.m that
return [0, 1] when passed [-1, 1].

Why your implementation works for scalars is that 0 is treated as false
by if.

On Tue, 2006-07-11 at 17:37 -0400, Pat Ferguson wrote:
> I have written a simple function:
>
>
>
> function  retval = killnegatives(x)
>
>      retval = x;
>
>      if(retval<0) retval = 0;
>
>      endif;
>
> endfunction
>
>
>
> and placed it in a file called “killnegatives.m”.
>
>
>
> I then fired up octave and this is what happens when I use my
> function.
>
>
>
>
>
> GNU Octave, version 2.1.73 (i686-pc-cygwin).
>
> Copyright (C) 2006 John W. Eaton.
>
> This is free software; see the source code for copying conditions.
>
> There is ABSOLUTELY NO WARRANTY; not even for MERCHANTIBILITY or
>
> FITNESS FOR A PARTICULAR PURPOSE.  For details, type `warranty'.
>
>
>
> http://www.octave.org.
>
>
>
> Please contribute if you find this software useful.
>
>
>
>
>
> http://www.octave.org/bugs.html to learn how to write a helpful
> report).
>
>
>
> octave:1> T = [-1,1]
>
> T =
>
>
>
>   -1   1
>
>
>
> octave:2> killnegatives(T)
>
> ans =
>
>
>
>   -1   1
>
>
>
> octave:3> killnegatives(-1)
>
> ans = 0
>
> octave:4> killnegatives (1)
>
> ans = 1
>
>
>
>
>
> The function seems to work as I expected when called with a single
> value, but not with an array.  I am sure the answer to why is obvious
> to someone and I hope you’ll share.
>
>
>
>
>
>
> _______________________________________________
> Help-octave mailing list