matlab取整 四舍五入
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函数功能如下:
ROUNDN Round numbers to specified power of 10
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
of tens position. For example, n = -2 rounds the input data to
the 10E-2 (hundredths) position.
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为字符型变量。
转:http://bbs.seu.edu.cn/pc/pccon.php?id=950&nid=15024&order=&tid=
matlab文本输出
两个函数:disp
fprintf
1、函数disp只带一个变量,他可以是自负矩阵或数值矩阵,要输出简单的文字信息,只需要用单引号将信息括起来:
>>disp(‘my favorite color is red’);
或者
>>yourname=input(‘enter your name’,’s’);
>>disp([‘your name is’,youname]);
例如
>> yourname = input('enter your name ','s');
enter your name panrq
>> disp(['your name is ',yourname]);
your name is panrq
选择带数值变量值的文本信息时,需要用函数num2str将数值变量的类型转换字符型
>> x=98;
>> outstring = ['x = ',num2str(x)];
>> disp(outstring);
x = 98
>> disp(['x = ',num2str(x)]);
x = 98
disp函数只能带一个变量,表格中的各列需奥组合成一个矩阵,如下面的程序所示。
>> x=0:pi/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.5878
3.1416 0.0000
Format命令
控制显示模式,直到下一个format出现前,这条format命令一直有效。
>> x=1.23456789;
>> format short;disp(pi);
3.1416
>> format long;disp(pi);
3.141592653589793
>> format short e;disp(pi);
3.1416e+000
>> format +;disp(pi);
+
>> format bank;disp(pi);
3.14
2、函数fprintf
fprintf(format);
fprintf(format,variables);
fprintf(fid,format,variables);
例如:
>> fprintf('i am concreten');
i am concrete
>> a=3;b='s';
>> fprintf('this is a %d and %s n',a,b);
this is a 3 and s
matlab取整 四舍五入的更多相关文章
- matlab取整
matlab取整 Matlab取整函数有: fix, floor, ceil, round.取整函数在编程时有很大用处.一.取整函数1.向零取整(截尾取整)fix-向零取整(Round towards ...
- iOS 常用的向上,向下取整, 四舍五入函数
向上取整:ceil(x),返回不小于x的最小整数; 向下取整:floor(x),返回不大于x的最大整数; 四舍五入:round(x) 截尾取整函数:trunc(x)
- JS取整,四舍五入,取绝对值等Math对象常用方法
function f1(type,num1) { switch(type) { case 'floor': return Math.floor(num1);//取整或下舍入 break; case ' ...
- js玩转数字----取整,四舍五入,数字字符串转换
取整: 向下取整Math.floor(),向上取整Math.ceil(),四舍五入Math.round()),保留有效数位n.toFixed(),产生大于等于0小于1的随机数Math.random() ...
- sql 向上取整 向下取整 四舍五入的实例;
SELECT CEILING(23.5/4)'向上取整' ---6 :SELECT FLOOR(23.5/4)'向下取整' ---5 :SELECT ROUND(23.5/4,1)'四舍五入' --5 ...
- 怎样对小数进行向上取整 / 向下取整 / 四舍五入 / 保留n位小数 / 生成随机数
1. 向上取整使用: Math.ceil() Math.ceil(0.1); Math.ceil(1.9); 2. 向下取整使用: Math.floor() Math.floor(0.1); Math ...
- JQ向上取整 和向下取整 四舍五入
向上取整 var a = 23.2325236 var abc = Math.ceil(a); //注意:Math.ceil(a)不要单独写一行,否则向上取整失败 abc = 24; ...
- python取整函数 向上取整 向下取整 四舍五入
向上取整 >>> import math >>> math.ceil(3.5) 4 >>> math.ceil(3.4) 4 >>&g ...
- c# 三种取整方法 向上取整 向下取整 四舍五入
Math.Round:四舍六入五取整 Math.Ceiling:向上取整,只要有小数都加1 Math.Floor:向下取整,总是舍去小数
随机推荐
- CentOS添加新硬盘到新的分区(xfs/ext4) 或者添加新分区
CentOs添加新硬盘到新的分区(xfs/ext4) 添加新分区 转载请注明:http://www.cnblogs.com/juandx/p/5618162.html 这篇文章介绍怎么添加一块新的硬 ...
- Flex各类型坐标转换(全局、本地、内容坐标间转换)
Flex包含3种坐标:全局坐标.本地坐标.内容坐标 全局坐标:stage级别,坐标原点为舞台的左上角,如MouseEvent的stageX.stageY坐标. 本地坐标:组件级别的坐标系,相对坐标,坐 ...
- ksvcreate: Process(m000) creation failed
一测试服务器数据库(Oracle Database 10g Release 10.2.0.5.0 - 64bit Production)突然访问不了,检查发现数据库处于挂起模式(hang mode), ...
- 使用dig查询dns解析
原文地址:使用dig查询dns解析 作者:chenwenming 一般来说linux下查询域名解析有两种选择,nslookup或者dig,而在使用上我觉得dig更加方便顺手. 如果是在debian下的 ...
- canvas作为背景
比如canvas的id是HB, 画好后执行document.body.style.background = "url('"+HB.toDataURL()+"')" ...
- .net 操作sftp服务器
因为项目的需要,整理了一段C#操作sftp的方法. 依赖的第三方类库名称为:SharpSSH 1.1.1.13. 代码如下: 1: using System; 2: using System.Coll ...
- plsql+绿色版oracle连接远程数据库配置及提示缺少msvcr71.dll解决方法
之前一直用的sqldeveloper连接oracle数据库,这个免费而且也是官方出品,除了体积略大启动略慢外,也没什么不好的.. 一次偶然机会决定试一下plsql,整理一下安装资料,需要本地oracl ...
- css3 选择器(二)
接css3选择器(一) 八.结构性伪类选择器[:nth-child(n)] :nth-child(n)选择器用来匹配某个父元素的一个或多个特定的子元素,和jquery中一样. 其中"n&qu ...
- OpenXml入门---word里面插入图片
下面介绍如何在word里面插入图片,顺便小弟发现MSDN官网有完整的OpenXML教程,虽然是全英文的不过还是很有帮助的. Tips,原来摘抄代码里面没有模板,在copy过来发现插入word中的图片大 ...
- 从〇开始构架前端(NLDV框架)
从〇开始构架前端(NLDV框架) 框架 设计模式 摘要:一个普通应用,大到微信, 小到豆瓣FM,必不可少的都包括四部分:Network.Logic.Data.View(NLDV).如何把他们组合起来, ...