From help-request at octave dot org Mon Jan 24 08:23:44 2005 Subject: Re: if statement within function From: To: "Ahn Kyung" , Date: Mon, 24 Jan 2005 09:21:51 -0500 ----- Original Message ----- From: "Ahn Kyung" To: Sent: Monday, January 24, 2005 00:57 Subject: if statement within function > I'm having trouble implementing if statement in a function. Here's an > example: > --------------------- > function y=f(x) > if (x>1) > y=1; > else > y=x.^2; > endif > endfunction > > x=[1;2]; Looks like a typo. Should this be x=[1:2]; ? > > octave:3> f(x) > ans = > > 1 > 4 > > octave:4> f(1) > ans = 1 > octave:5> f(2) > ans = 1 > -------------------- > You see? f(2)=1, as expected, but if I use a vector x=[1;2];, I get > f(x)=[1;4]; When using a vector, the if statement is useless... > > I don't want to pass each element, but want to use f(x) as a chunk, with a > given vector x. What should I do to cure this problem?? > > Thank you in advance. > > > > > ------------------------------------------------------------- > Octave is freely available under the terms of the GNU GPL. > > Octave's home on the web: http://www.octave.org > How to fund new projects: http://www.octave.org/funding.html > Subscription information: http://www.octave.org/archive.html > ------------------------------------------------------------- > ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------