backfire1986 发表于 2009-4-5 11:46

请教一个if结构问题。

程序如下:
x=0.1:0.05:5;
if x<1
y=-atan((2*0.1*x)./(1-x.^2))
else
y=-pi-atan((2*0.1*x)./(1-x.^2))
end
为什么每次运行,即使在x<1段,y的值都是-pi-atan((2*0.1*x)./(1-x.^2)),if语句不起作用?

friendchj 发表于 2009-4-5 13:10

help if

Nonscalar Expressions
If the evaluated expression yields a nonscalar value, then every element of this value must be true or nonzero for the entire expression to be considered true. For example, the statement if (A < B) is true only if each element of matrix A is less than its corresponding element in matrix B.

backfire1986 发表于 2009-4-5 17:19

明白了,那就再加个循环好了。
x=0:0.05:5;
for i=1:length(x);
   if x(i)<1, y(i)=-atan((2*0.1*x(i))/(1-x(i)^2));
   elseif x(i)==1, y(i)=-pi/2;
   else y(i)=-pi-atan((2*0.1*x(i))/(1-x(i)^2));
   end
end

[ 本帖最后由 ChaChing 于 2009-4-5 20:36 编辑 ]
页: [1]
查看完整版本: 请教一个if结构问题。