[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: problem with function
From: |
Paul Kienzle |
Subject: |
Re: problem with function |
Date: |
Mon, 14 Mar 2005 11:23:56 -0500 |
The conditional:
if (t >= 0 && t <= periode/2)
is equivalent to:
if (all(t>=0) && all(t <= periode/2))
The following is a mechanical translation
to do what you want:
y = zeros(size(t));
idx = t>=0 & t<= periode/2;
y(idx) = t(idx)./(-periode/2) + 1;
y(~idx) = t(~idx)./(-periode/2) + 2;
In this particular case, though, you can do the following:
y = t./(-periode/2) + 1;
y(t<0 | t>periode/2) += 1;
or even:
y = t./(-periode/2) + 1 + (t<0 | t>periode/2);
- Paul
On Mar 14, 2005, at 11:04 AM, Arnau Mir Torres wrote:
Hello.
I've defined the following function:
function y = serra(t)
global periode
if (t >= 0 && t <= periode/2)
y = t./(-periode/2) +1;
else
y = t./(-periode/2) +2;
endif
endfunction
If I do:
global periode = 200;
t=(0:10:200)';
and I try to print
serra(t), octave responses:
2.00000
1.90000
1.80000
1.70000
1.60000
1.50000
1.40000
1.30000
1.20000
1.10000
1.00000
0.90000
0.80000
0.70000
0.60000
0.50000
0.40000
0.30000
0.20000
0.10000
0.00000
but I think this is wrong, because when t is between 0 and 100,
serra(t) has a value between 1 and 0.
Moreover, if I do:
for i=1:length(t)
y(i)=serra(t(i));
endfor
and next I do
y
octave responses the correct answer:
1.00000
0.90000
0.80000
0.70000
0.60000
0.50000
0.40000
0.30000
0.20000
0.10000
0.00000
0.90000
0.80000
0.70000
0.60000
0.50000
0.40000
0.30000
0.20000
0.10000
0.00000
So, my question is:
why octave does this? Is this a bug?
Thanks,
Arnau.
--
Arnau Mir Torres
Director Departament Matemà tiques i Informà tica.
Universitat de les Illes Balearss.
Edifici A. Turmeda, Campus de la UIB.
Crta. de Valldemossa, km. 7,5.
07122 Palma de Mallorca.
Baleares.
Spain.
Phone: +34 971 172987
Fax: +34 971 173003
e-mail: address@hidden
web: http://dmi.uib.es/~arnau
-------------------------------------------------------------
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
-------------------------------------------------------------