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取整的更多相关文章

  1. matlab取整 四舍五入

    Matlab取整函数有: fix, floor, ceil, round.取整函数在编程时有很大用处.一.取整函数1.向零取整(截尾取整)fix-向零取整(Round towards zero):&g ...

  2. ios小数向上、下取整,计算结果向上、下取整

    [摘要:小数背上与整,指小数局部间接进1 x=3.14, ceilf (x)=4 小数背下与整,指间接往失落小数局部 x=3.14,floor(x)=3 盘算效果背上与整 A被除数,B除数 ,(AB- ...

  3. Java Math 取整的方式

    1.Math.floor floor,英文原意:地板. Math.floor 函数是求一个浮点数的地板,就是 向下 求一个最接近它的整数,它的值肯定会小于或等于这个浮点数. 2.Math.ceil c ...

  4. c#中取整,向上取,向下取

    Math.Ceiling()向上取整, Math.Floor()向下取整 示例: d = 4.56789 Math.Ceiling(Convert.ToDecimal(d)).ToString();M ...

  5. iOS 常用的向上,向下取整, 四舍五入函数

    向上取整:ceil(x),返回不小于x的最小整数; 向下取整:floor(x),返回不大于x的最大整数; 四舍五入:round(x) 截尾取整函数:trunc(x)  

  6. Sql 获取向上取整、向下取整、四舍五入取整的实例

    [四舍五入取整截取] select round(54.56,0) [向下取整截取] SELECT FLOOR(54.56) [向上取整截取]  SELECT   CEILING(13.15) --MS ...

  7. js只保留整数,向上取整,四舍五入,向下取整等函数

    1.丢弃小数部分,保留整数部分 parseInt(5/2) 2.向上取整,有小数就整数部分加1 Math.ceil(5/2) 3.四舍五入. Math.round(5/2) 4.向下取整 Math.f ...

  8. javascript 取整,取余数

    1.丢弃小数部分,保留整数部分 parseInt(5/2) 2 2.向上取整,有小数,则整数部分加1 Math.ceil(5/2) 3 3.四舍五入 Math.round(5/2) 3 4.向下取整 ...

  9. MATLAB取余求模

    (1)fix(x) : 截尾取整 >> fix( [3.12 -3.12]) ans =   3   -3 (2)floor(x): 不超过x 的最大整数.(高斯取整) >>  ...

随机推荐

  1. 工厂模式 - 程序实现(java)

    09年5月CSDN一网友提出如下问题: 设计一个用于管理银行客户的类BankCustomer: 仅描述客户的几个重要方面: 帐号.身份证号.姓名.联系方式.密码.账户余额. 所有的成员变量均用priv ...

  2. Java基础知识强化之集合框架笔记19:List集合迭代器使用之 并发修改异常的产生原因 以及 解决方案

    1. 我有一个集合,如下,请问,我想判断里面有没有"world"这个元素,如果有,我就添加一个"javaee"元素,请写代码实现. ConcurrentModi ...

  3. sqlite使用blob类型存储/访问 结构体

    /* open fire host and slora report data database */ int open_report_db(void) { ; char sql[SQL_COMMAN ...

  4. (总结)CentOS Linux搭建SVN Server配置详解

         PS:虽然在公司linux服务器上搭建过几次svn,但是时间长了,有些配置操作会忘掉,上网搜索的结果都不大满意,有幸在前几天看到一篇算是最满意的svn搭建文章,转载一下以备以后使用,原文地址 ...

  5. c# 的导入功能SqlBulkCopy

    private static void DataTableToSQLServer( DataTable dt) { string connectionString = GetConnectionStr ...

  6. 内网映射到公网工具 --- ngrok

    ngrok可以将内网映射到公网上,这样就可以在公网上访问你的网络服务. 该工具通常在进行app开发和微信开发时比较有用,这样就可避免在公网服务器上单独部署项目,通过映射,直接连接本地服务即可进行开发. ...

  7. java '相等'的比较.

    我们知道对于操作符 "==",如果比较的是原生类型(primitive type),表示的是 '值本身'是否相等;而对于引用类型(reference type),表示的是 '对象的 ...

  8. org.springframework.beans.factory.BeanCreationException: 求教育!

    2014-11-26 14:05:56 [org.springframework.web.context.support.XmlWebApplicationContext]-[WARN] Except ...

  9. CentOS 6.5 64位,调整分区大小

    调整硬盘分区大小 想增加root空间,减少home空间. 1.查看硬盘使用情况. [root@npm ~]# df -h Filesystem Size Used Avail Use% Mounted ...

  10. OpenCart本地调试环境搭建

    OpenCart简介: 免费开源网络版电子商务系统,是建立在线商务网站首选之一.有众多用户和开发基础,结合其丰富特性与模板插件,可最大化定制在线商店.(也就是用来方便开网店的) 本地调试准备: Fir ...