[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: function values vector
From: |
John W. Eaton |
Subject: |
Re: function values vector |
Date: |
Mon, 17 May 2010 10:35:19 -0400 |
On 17-May-2010, Francesco Potortì wrote:
| >I have a vector x=linspace(0,2*pi,256). There is a function H given as
| >follows:
| >
| >function y=H(x)
| > if (x<pi)
| > y=0;
| > else
| > y=1;
| > endif
| >end
| >
| >Now, I want a vector Y such that each of its coordinates xi is H(xi). What's
| >the best way to that? Y=H(X) doesn't work (it makes Y=1, which I don't
| >understand).
|
| Ah, and I don't know why you get 1, I would have expected an error. In
| fact, the 'if' condition is (x<pi), which is a bool matrix instead of a
| single bool value. I don't know why Octave does not give an error here,
| and why it is evalued as false.
It happens because if X is an array, X < PI returns a logical array
of the same size as X, and that is only true in the condition of an IF
statement if all the values in the array are true.
jwe