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函数功能如下:
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为字符型变量。
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.1416format long;disp(pi);
3.141592653589793format short e;disp(pi);
3.1416e+000format +;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 concretea=3;b=’s’;
fprintf(‘this is a %d and %s n’,a,b);
this is a 3 and s
MATLAB取整操作相关推荐
- python矩阵所有元素取整_Python中如何对一个数值进行取整操作呢?
摘要: 下文讲述Python中数值取整的方法分享,如下所示: 数值取整是我们处理数据常用的方法, 那么Python如何对数据进行取整操作呢? 下文将一一道来,如下所示: 实现思路: 方式1: math ...
- Python 中的取余与取整操作
%: 对于正数的取余操作,跟其他的语言没有什么区别,在这里重点说一下负数的取余: print (-5%3)#输出 1#仅有一负号时,在负无穷到-5之间找到一个数能被3整数,最接近于-5的数是-6,所以 ...
- C++:取整操作ceil()、floor()、round()
取整操作ceil().floor().round() 在数学计算中有三种常用的取整操作:向上取整.向下取整.四舍五入取整,在 C++ 中分别通过 ceil().floor().round() 三个函数 ...
- Java | 取整操作
java 中取整操作提供了四种方法: 第一种:ceil是天花板的意思,表示向上取整.public static double ceil(double a)//向上取整 第二种:floor是地板的意思, ...
- matlab取整 四舍五入
matlab取整 四舍五入 2011-12-13 11:05 Matlab取整函数有: fix, floor, ceil, round.取整函数在编程时有很大用处. 一.取整函数 1.向零取整(截尾取 ...
- 转载--数据库sql取整操作
转自:http://blog.csdn.net/outget1/article/details/4821975 用oracle sql对数字进行操作: 取上取整.向下取整.保留N位小数.四舍五入.数字 ...
- MATLAB取整运算
MATLAB的五种取整运算 1 按照四舍五入的规则取整 形式:round(x) >> round(3.6) ans =4 ------------ >> round(3.4) ...
- matlab取整指令,matlab取整函数命令代码
描述 1)fix(x) : 截尾取整. >> fix( [3.12 -3.12]) ans = 3 -3 (2)floor(x):不超过x 的最大整数.(高斯取整) >> fl ...
- matlab取整与取余
Matlab取整函数有: fix, floor, ceil, round.具体应用方法如下: fix朝零方向取整,如fix(-1.3)=-1; fix(1.3)=1; floor,顾名思义,就是地板, ...
- 【趣谈】JavaScript取整操作
1.Math.floor 向下取整 需要用一次Math对象,语法看起来很冗余,但其实是最建议用的. number -> int 2.parseInt 虽然看起来是转Int ...
最新文章
- 【杭电ACM】1097 A hard puzzle
- 集成ShareSDK里报错NSConcreteMutableData wbsdk_base64EncodedString]
- Fortify:五大SOA架构都有安全漏洞
- [CLR via C#]12. 泛型
- Android NDK学习笔记5:引用类型管理
- .net Framework各个版本之间的发展
- 论文阅读 - Group Normalization
- 分布式深度学习,93页ppt概述最新DDL技术发展
- qemu-img创建qcow2虚拟磁盘的预分配策略
- LeetCode(657)——机器人能否返回原点(JavaScript)
- codable swift_使用Codable进行Swift JSON解析
- Linux下passwd和shadow文件内容详解
- 《神探tcpdump第八招》-linux命令五分钟系列之四十二
- [渝粤教育] 四川信息职业技术学院 Linux网络管理 参考 资料
- 控制继电器(esp32+继电器)
- python scipy拟合曲线optimize.curve_fit 50例
- 世界线(bzoj2894)(广义后缀自动机)
- 上市公司产权和股权性质-区分非国企、国企和央企(2003-2020)
- Switch模拟器调研
- 几种常用的power bi 图表怎么做