
matlab if语句的用法
i=1:20
x=(0.95+0.1*i/21)*1.0
if x<1
y=(x-0.95)/0.05
else x>1
y=(1.05-x)/0.05
end
算出来怎么是 0.9548 0.9595 0.9643 0.9690 0.9738 0.9786 0.9833 0.9881 0.9929 0.9976 1.0024
1.0071 1.0119 1.0167 1.0214 1.0262 1.0310 1.0357 1.0405 1.0452跟实际算出来的值不一样。
我指的是实际算出来的答案应该是0.0960 0.1900 0.2860 0.3800 0.4760 0.5720 0.6660 0.7620 0.8580 0.9520 0.9520 0.8580 0.7620 0.6660 0.5720 0.4760 0.3800 0.2860 0.1900 0.0960,为什么不一样呢? 展开
嵌套if语句的语法如下:
if <expression 1>
% Executes when the boolean expression 1 is true
if <expression 2>
% Executes when the boolean expression 2 is true
end
end
例如:
创建脚本文件并在其中键入以下代码 :
a = 100;
b = 200;
% check the boolean condition if( a == 100 )
% if condition is true then check the following
if( b == 200 )
% if condition is true then print the following
fprintf('Value of a is 100 and b is 200\n' );
end
end
fprintf('Exact value of a is : %d\n', a );
fprintf('Exact value of b is : %d\n', b );MATLAB
执行上面示例代码,得到以下结果:
Value of a is 100 and b is 200
Exact value of a is : 100
Exact value of b is : 200
扩展资料:
C语言提供了三种形式的if语句:
1、if(表达式)语句。
例如:
if(x>y)printf("%d",x);2、if(表达式)语句1 else 语句2。
例如:
if(x>y)printf("%d",x);
else printf("%d",y);
3、在每个语句中,可以有多个语句,但需要加上大括号。
例如:
if(x>y){printf("%d",x);break;}参考资料来源:百度百科—if语句
2009-11-20
要么这样改写,结果还是一样的,个人感觉容易理解:
y=ones(1,20);
for =1:20
x=(0.95+0.1*i/21)*1.0
if x<1
y(1,i)=(x-0.95)/0.05;
else x>1
y(1,i)=(1.05-x)/0.05;
end
end
y
i=1:20;
x=(0.95+0.1*i/21)*1.0;
y=zeros(1,20);
if x<1
y=y+(x-0.95)/0.05;
else
y=y+(1.05-x)/0.05;
end
y
结果:一行矩阵
1.9047 1.8095 1.7142 1.6190 1.5238 1.4285 1.3333 1.2380 1.1428 1.0476 0.9523 0.8571 0.7619 0.6666 0.5714 0.4761 0.3809 0.2857 0.1904 0.0952
for i=1:20
x(i)=(0.95+0.1*i/21)*1.0;
if x(i)<1
y(i)=(x(i)-0.95)/0.05;
else
y(i)=(1.05-x(i))/0.05;
end
end
y