博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Matlab取整
阅读量:6858 次
发布时间:2019-06-26

本文共 2217 字,大约阅读时间需要 7 分钟。

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

 
 
 

转载于:https://www.cnblogs.com/jhonsonwanger/p/4377982.html

你可能感兴趣的文章
设置域用户帐户密码策略
查看>>
C语言for循环的一些注意细节
查看>>
linux下top命令参数解释
查看>>
输出100000以内的质数,并计算时间 学习笔记
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
hadoop+hbase+zookeeper+spark+phoenix相关实施报错处理
查看>>
.Net连接Oracle数据库的实现代码
查看>>
Unity3D客户端实时同步
查看>>
我和小美的撸码日记--基于MVC+Jqgrid的.Net快速开发框架
查看>>
企业购买邮件服务器如何防忽悠
查看>>
如何让echo显示的内容是带颜色的
查看>>
webstorm + Git 配置与使用
查看>>
sqlserver 2012中实现字符串连接的新方法
查看>>
电脑技术员联盟 Ghost Xp Sp3 装机版V5.1(大地作品)
查看>>
哥活的快乐,跳的寂寞
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
克隆虚拟机
查看>>