声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 861|回复: 2

[编程技巧] 请教一个if结构问题。

[复制链接]
发表于 2009-4-5 11:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
程序如下:
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语句不起作用?
回复
分享到:

使用道具 举报

发表于 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.

评分

1

查看全部评分

 楼主| 发表于 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

查看全部评分

您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-6-29 21:30 , Processed in 0.092004 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表