如何在MATLAB中更改浮点精度将5.145556666视为5.24 [英] How change percision of float point in matlab treat 5.145556666 as 5.24

查看:182
本文介绍了如何在MATLAB中更改浮点精度将5.145556666视为5.24的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

**已更新 我想在matlab.as的float部分中将float或double只用2位数字作为示例

**updated I want treat float or double only with 2 digit in float section in matlab.as example

a=23.1234443434343434454545444;

我想如果我使用它等于

a=23.12;

在MATLAB中有任何想法吗?

any idea in matlab?

我想要在Matlab中使用 round(1.95583,2);//返回1.95 (php function)之类的函数

I want funcion like round(1.95583, 2);//return 1.95 (php function ) in matlab

推荐答案

如果您只想显示仅2位数字的值,则可以

If you want to display values with only 2 digits, you can do

>> format bank
>> pi
ans = 
    3.14
>> rand(4)
ans =
      0.81    0.63    0.96    0.96
      0.91    0.10    0.96    0.49
      0.13    0.28    0.16    0.80
      0.91    0.55    0.97    0.14

这确实 NOT 意味着计算将以较低的精度进行.

This does NOT mean calculations will be carried out with less precision.

如果后者确实是您想要的(为什么?!),则可以使用类似的构造

If the latter is really what you want (why?!), you could use constructions like

ppi = single(pi)

降低精度,或

f = @(x) double(uint64(x*100))/100;

用于保证两位数精度.在最后一种情况下,必须在使用所有值之前通过函数f进行传递:

for guaranteed 2-digit precision. In the last case, you have to pass all values through the function f prior to using them:

>> ppi = f(pi)
ans = 
    3.140000000000000
>> f(rand(4)) 
ans =
   0.280000000000000   0.690000000000000   0.440000000000000   0.190000000000000
   0.050000000000000   0.320000000000000   0.380000000000000   0.490000000000000
   0.100000000000000   0.950000000000000   0.770000000000000   0.450000000000000
   0.820000000000000   0.030000000000000   0.800000000000000   0.650000000000000

如果您正在为后一种情况寻找更优雅的解决方案,请使用Danil建议的定点工具箱.

If you're looking for a more elegant solution for this last case, use the fixed-point toolbox, as Danil suggested.

这篇关于如何在MATLAB中更改浮点精度将5.145556666视为5.24的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆