[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
if statement within function
From: |
Ahn Kyung |
Subject: |
if statement within function |
Date: |
Mon, 24 Jan 2005 05:57:17 +0000 |
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];
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
-------------------------------------------------------------
- if statement within function,
Ahn Kyung <=