头条 科技 产经 家电 智能 手机 芯片 数码 电商 WAP版
中国IT产经新闻网-移动互联网与智能搜索领域是未来IT产业发展的趋势!
中国IT产经新闻/智能制造/正文
matlab四舍五入各种情况的使用方法介绍如下所示
来源:
2023-08-18
编辑:晓露

     Matlab取整函数有: fix, floor, ceil, round.取整函数在编程时有很大用处。


一、取整函数
   1.向零取整(截尾取整)
fix-向零取整(Round towards zero);
 >> fix(3.6)  
 ans =3
2.向负无穷取整(不超过x 的最大整数-高斯取整)
floor-向负无穷取整(Round towards minus infinity);
>> floor(-3.6)  
ans =-4
3.向正无穷取整(大于x 的最小整数)
ceil-向正无穷取整(Round towards plus infinity);
>> ceil(-3.6)   
ans =-3
4.向最近整数取整,四舍五入(四舍五入取整)
round-向最近整数取整,四舍五入(Round towards nearest integer);
>> round(3.5)
ans =4  
二、在小数点后某一位四舍五入,即保留几位小数,也经常用到。
1.数值型 roundn—任意位位置四舍五入
>>a=123.4567890;
>>a=roundn(a,-4)
a =123.4568
其中roundn函数功能如下:   
y = ROUNDN(x) rounds the input data x to the nearest hundredth.   %不指定n,精确到百分位 y = ROUNDN(x,n) rounds the input data x at the specified power    %精确到小数点后指定位数n 
2.符号型
digits(4)
vpa(....)
必须说明:vpa命令不能识别整数与小数,只算总位数,因此对它来说小数整数无论哪个都占一位,例如对9.3154保留两位小数时就得写成:
>>a=9.3154;
>>digits(3)
>>b=vpa(a)
b=9.32
其中b为符号型变量;
3.字符型
>>a=12.34567;
>>b = sprintf('%8.2f',a)
b = 12.35 其中b为字符型变量。

产业点评更多
厂商动态更多
热门综合更多
CopyRight @ 2008-2023 中国IT产经新闻网 All Right Reserved 违者必究 湘ICP备2022017330号-2