Matlab取整函数有: fix, floor, ceil, round.取整函数在编程时有很大用处。
fix-向零取整(Round towards zero);
>> fix(3.6)
ans = 3
floor-向负无穷取整(Round toSEO靠我wards minus infinity);
>> floor(-3.6)
ans = -4
ceil-向正无穷取整(Round towards plus inSEO靠我finity);
>> ceil(-3.6)
ans = -3
round-向最近整数取整,四舍五入(Round towards nearest inteSEO靠我ger);
>> round(3.5)
ans = 4
>>a=123.4567890;
>>aSEO靠我=roundn(a,-4)
a = 123.4568
其中roundn函数功能如下:
y = ROUNDN(x) rounds the input data x to the nearest hundrSEO靠我edth. %不指定n,精确到百分位 y = ROUNDN(x,n) rounds the input data x at the specified power %精确到小数点后指定位数nSEO靠我
digits(4)
vpa(....)
必须说明:vpa命令不能识别整数与小数,只算总位数,因此对它来说小数整数无论哪个都占一位,例如对9.3154保留两位小数时就得写成:
>>a=9.3154;SEO靠我
>>digits(3)
>>b=vpa(a)
b= 9.32
其中b为符号型变量;
>>a=12.34567;
>>b = sprintf(%8.2f,a)
b = 12.35 其中b为SEO靠我字符型变量。
两个函数:disp
fprintf
>>disp(‘my favSEO靠我orite color is red’);
或者
>>yourname=input(‘enter your name’,’s’);
>>disp([‘your name is’,youname]);
例如
>>SEO靠我 yourname = input(enter your name ,s);
enter your name panrq
>> disp([your name is ,yourname]);
your naSEO靠我me is panrq
选择带数值变量值的文本信息时,需要用函数num2str将数值变量的类型转换字符型
>> x=98;
>> outstring = [x = ,num2str(x)];
>> disp(SEO靠我outstring);
x = 98
>> disp([x = ,num2str(x)]);
x = 98
disp函数只能带一个变量,表格中的各列需奥组合成一个矩阵,如下面的程序所示。
>> x=0:pi/SEO靠我5:pi;y=sin(x);
>> disp([x y]);
0 0
0.6283 0.5878
1.2566 0.9511
1.8850 0.9511
2.5133 0.SEO靠我5878
3.1416 0.0000
Format命令
控制显示模式,直到下一个format出现前,这条format命令一直有效。
>> x=1.23456789;
>> format short;disSEO靠我p(pi);
3.1416
>> format long;disp(pi);
3.141592653589793
>> format short e;disp(pi);
3.1416e+000
>> formatSEO靠我 +;disp(pi);
+
>> format bank;disp(pi);
3.14
fprintf(format);
fprintf(format,variables);
fprintSEO靠我f(fid,format,variables);
例如:
>> fprintf(i am concreten);
i am concrete
>> a=3;b=s;
>> fprintf(this is a %SEO靠我d and %s n,a,b);
this is a 3 and s
网站备案号:浙ICP备17034767号-2