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:向下取整,总是舍去小数
随机推荐
- PreferenceScreen监听子项的刷新
有个PreferenceScreen,他有一些个子项目.它的Summary需要根据子项的设置来改变的,所以需要监听子项的刷新事件. preferenceScreen.setOnPreferenceCh ...
- ORA-00988: missing or invalid password(s)
创建账号或修改账号密码时有可能会遇到ORA-00988: missing or invalid password(s),那么什么情况下会遇到这种错误呢? 一般是因为密码的设置不符合命名规范: 1:密码 ...
- Mysql hql字符串字段中是否包含某个字符串,用 find_in_set
有这样一个需求,在Mysql数据库字符串字段(权限)中,有范围在 1 到 N 之间代表不同权限的值,分别被','分开,现在要取出具有某权限的所有成员列表. 创建表: 1 CREATE TABLE us ...
- hibernate 实现分页查询语句、单条查询语句、多条查询语句、修改、删除语句
package com.hanqi.test; import java.util.Date; import java.util.List; import org.hibernate.Query; im ...
- mysql错误一例:ERROR 1030 (HY000): Got error 28 from storage engine
在使用mysqldump导出一份建库脚本是,发生了下面的错误: 当执行 desc table_name; 时也报错: tag为表名,show index from tag;倒是可以执行. 其实真正的错 ...
- C#读取XML文件
--硬盘Xml文件存储路径:d:\xmlFile\Testxml.xml xml文件内容: <Root> <Tab> <ID>245575913</ID> ...
- cocosstdio之字体之文本和FNT字体
FNT字体和文本字体的作用是:导入字体资源可以使用字体资源便可以使用其资源内的字体来在程序中使用 不同的是FNT字体资源内容比较少,所以个人猜想可以在特定情况下使用: 两种字体资源对比: 赋值过程对比 ...
- 聚合及UML表示
聚合聚合是一种特别类型的关联,用于描述“总体到局部”的关系. 聚合分成: 基本聚合与合成聚合 基本聚合: 基本聚合一般也简称为聚合(Aggregation).在基本的聚合关系中, 部分类(B) ...
- 树莓派搭建安装mysql
最近刚入手了一枚树莓派,突发奇想打算做一个小型的家用服务器,在家7*24小时一直挂着. 真的是非常小,只有巴掌大,给树莓派买了一些配件,外壳.小风扇.2片散热片.32G SD卡.HDMI线,组装之后的 ...
- line-height1.5和line-height:150%的区别
一.区别 区别体现在子元素继承时,如下: 父元素设置line-height:1.5会直接继承给子元素,子元素根据自己的font-size再去计算子元素自己的line-height. 父元素设置line ...