MATLAB中小数位数的设置方法

format:设置输出格式

对浮点性变量,缺省为format short.
format并不影响matlab如何计算和存储变量的值。对浮点型变量的计算,即单精度或双精度,按合适的浮点精度进行,而不论变量是如何显示的。对整型变量采用整型数据。整型变量总是根据不同的类(class)以合适的数据位显示,例如,3位数字显示显示int8范围 -128:127。format short, long不影响整型变量的显示。

  • format long 显示15位双精度,7为单精度(scaled fixed point)
  • format short 显示5位(scaled fixed point format with 5 digits)
  • format short eng 至少5位加3位指数
  • format long eng 16位加至少3位指数
  • format hex 十六进制
  • format bank 2个十进制位
  • format + 正、负或零
  • format rat 有理数近似
  • format short 缺省显示
  • format long g 对双精度,显示15位定点或浮点格式,对单精度,显示7位定点或浮点格式。
  • format short g 5位定点或浮点格式
  • format short e 5位浮点格式
  • format long e 双精度为15位浮点格式,单精度为7为浮点格式

Matlab里面显示的数字默认情况下是以short类型进行显示和存储的。但是有时候我们需要对它的显示格式(精度)进行更改,以适合我们的需求。更改方法如下:

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.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://blog.csdn.net/yf210yf/article/details/7235907

MATLAB中的小数点位数精度可以通过多种方式控制,主要包括数值显示的精度和数值计算的精度。 1. 数值显示精度:MATLAB默认情况下会显示四位小数,但你可以通过设置`format`函数来改变显示的小数位数。比如`format long`和`format short`分别用于设置长格式和短格式,长格式显示更多的小数位数,而短格式则显示更少。 2. 数值计算精度:MATLAB默认的浮点数精度大约是15个有效数字,这是因为MATLAB内部使用的是双精度(double)浮点数。对于特定的数值计算,如果需要更高的精度,可以使用MATLAB提供的高精度数值计算包,比如`vpa`函数(Variable Precision Arithmetic)来进行任意精度的数值计算。 3. 控制浮点数的精度:在某些情况下,你可能需要控制浮点数在运算中的精度,这时候可以使用`digits`函数来设置有效的数字位数。这会影响`vpa`函数以及其他高精度数值计算的精度。 下面是一些具体的代码示例: - 设置显示格式: ```matlab format short % 显示4位小数 format long % 显示15位小数 format short e % 科学计数法,显示4位小数 ``` - 使用`vpa`进行高精度计算: ```matlab digits(30); % 设置高精度数值计算的位数为30位 a = vpa('1.23456789123456789'); % 使用高精度进行计算 ``` - 控制浮点数的精度: ```matlab digits(10); % 设置浮点数精度为10位有效数字 x = 1.23456789123456789; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值