matlab取整

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

时间: 2024-08-29 21:30:59

matlab取整的相关文章

matlab取整 四舍五入

Matlab取整函数有: fix, floor, ceil, round.取整函数在编程时有很大用处.一.取整函数1.向零取整(截尾取整)fix-向零取整(Round towards zero):>> fix(3.6)   ans =     32.向负无穷取整(不超过x 的最大整数-高斯取整)floor-向负无穷取整(Round towards minus infinity):>> floor(-3.6)  ans =    -43.向正无穷取整(大于x 的最小整数)ceil-向

matlab取整与取余

Matlab取整函数有: fix, floor, ceil, round.具体应用方法如下:fix朝零方向取整,如fix(-1.3)=-1; fix(1.3)=1;floor,顾名思义,就是地板,所以是取比它小的整数,即朝负无穷方向取整,如floor(-1.3)=-2; floor(1.3)=1;floor(-1.8)=-2,floor(1.8)=1ceil,与floor相反,它的意思是天花板,也就是取比它大的最小整数,即朝正无穷方向取整,如ceil(-1.3)=-1; ceil(1.3)=2;

matlab 对矩阵取整的函数

Matlab取整函数有: fix, floor, ceil, round.取整函数在编程时有很大用处.一.取整函数1.向零取整(截尾取整)fix-向零取整(Round towards zero):>> fix(3.6)   ans =     32.向负无穷取整(不超过x 的最大整数-高斯取整)floor-向负无穷取整(Round towards minus infinity):>> floor(-3.6)  ans =    -43.向正无穷取整(大于x 的最小整数)ceil-向

MATLAB取整函数 fix floor ceil round

MATLAB取整函数 (1)fix(x) : 截尾取整. >> fix( [3.12 -3.12]) ans = 3    -3 (2)floor(x):不超过x 的最大整数.(高斯取整) >> floor( [3.12 -3.12]) ans = 3    -4 (3)ceil(x) : 大于x 的最小整数 >> ceil( [3.12 -3.12]) ans = 4    -3 (4)四舍五入取整 >> round(3.12 -3.12) ans = 0

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

Math.Ceiling()向上取整, Math.Floor()向下取整 示例: d = 4.56789 Math.Ceiling(Convert.ToDecimal(d)).ToString();Math.Ceiling(Convert.ToDouble(d)).ToString();Math.Floor(Convert.ToDecimal(d)).ToString(); Math.Floor(Convert.ToDouble(d)).ToString(); --记录铭心

SQL 向上取整、向下取整、四舍五入取整的实例!round、rounddown、roundup

sql server ==================================================== [四舍五入取整截取] select round(54.56,0) ==================================================== [向下取整截取] SELECT FLOOR(54.56) ==================================================== [向上取整截取] SELECT CE

java小数取整

java中提供三种小数取整方式 Math.ceil() Math.floor() Math.round() ceil:天花板,向上quzheng Math.ceil(11.5) = 12 Math.ceil(-11.5) = -11 floor:地,向下取整 Math.floor(11.5) = 11 Math.floor(-11.5) = -12 round:4舍5入 Math.round(x + 0.5),再向下取整 当 x = 11.5 时,Math.round(x + 0.5) = Ma

matlab取模与取余

mod函数采用floor,rem函数采用fix函数.那么什么是floor和fix? fix(x):截尾取整.如: >> fix([3.4 , -3.4]) ans = 3 -3 floor(x):高斯取整(不超过x的最大整数).如: >> floor([3.4 , -3.4]) ans = 3 -4 PS:顺便再说下另外两个取整函数ceil()和round() ceil(x) : 大于x 的最小整数.如: >> ceil([3.4 , -3.4]) ans = 4 -3

JavaScript 中对小数取整的常用函数

常见的js截取小数的方法 1.丢弃小数部分,保留整数部分 js:parseInt(7/2) 2.向上取整,有小数就整数部分加1 js: Math.ceil(7/2) 3,四舍五入. js: Math.round(7/2) 4,向下取整 js: Math.floor(7/2) 5.Number 四舍五入为指定小数位数的数字 js : 7.23.toFixed(num) . num参数为想要截取的小数位数