GNU Octave,将数字四舍五入为单位精度 [英] GNU Octave, round a number to units precision

查看:1055
本文介绍了GNU Octave,将数字四舍五入为单位精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在GNU Octave版本3.4.3中,我想像这样将矩阵的内容四舍五入为2个单位的精度.

In GNU Octave version 3.4.3 I want to round a matrix to 2 units precision on the contents of a matrix like this.

mymatrix=[1.1234567, 2.12345; 3.1234567891, 4.1234];
disp(mymatrix);

此打印:

1.1235   2.1235
3.1235   4.1234

如您所见,disp将精度强制为'5',我希望单位精度为2.我该怎么做?

As you can see, the disp forces the precision to '5', I want the units precision to be 2. How do I do this?

推荐答案

如何在Octave中四舍五入矩阵中的元素:

有许多不同的方法可以将矩阵四舍五入并以八度为单位四舍五入一个数字.

How to round off elements in a matrix in Octave:

There are many different ways to round a matrix and round a number in octave.

mymatrix=[100.1234567, 2.12345; 3.1234567891, 4.1234];
rows = rows(mymatrix);
cols = columns(mymatrix);
for i = 1:rows
  for j = 1:cols
    sprintf("%5.2f", mymatrix(j,i))
  endfor
endfor

输出,注意%5.2f"令牌. "f"表示期望为浮点数,"5"表示占据5个空格. 2表示小数点后2个单位的精度.

Output, note the "%5.2f" token. The 'f' means expect a float, the 5 means occupy 5 spaces. The 2 means 2 units precision after the decimal point.

ans = 100.12
ans =  3.12
ans =  2.12
ans =  4.12

选项2,使用eval和mat2str舍入到有效数字

mymatrix2=[100.1234567, 2.12345; 3.1234567891, 4.1234];
j = mat2str(mymatrix2, 3);
mymatrix2=eval(j)

输出,矩阵四舍五入为3个有效数字,注意100.123舍入为100,而2.12345舍入为2.12

Output, matrix rounded to 3 significant digits, notice 100.123 rounded to 100 while the 2.12345 was rounded to 2.12

mymatrix2 =
   100.0000     2.1200
     3.1200     4.1200

选项3,使用取整功能

round函数在Octave中没有精度参数.但是,您可以通过将矩阵中的每个项目乘以100,将其四舍五入到最接近的整数,然后将每个项目除以100来解决:

Option 3, use the round function

The round function does not have a precision parameter in Octave. However you can hack around it by multiplying each item in the matrix by 100, rounding it to the nearest int, then dividing each item by 100:

mymatrix=[100.1234567, 2.12345; 3.1234567891, 4.1234];
round(mymatrix .* 100) ./ 100

输出,正确进行舍入:

ans =
   100.1200     2.1200
     3.1200     4.1200

选项4,指定output_precision(num)

您注意到上面的选项3保留了结尾的零,这可能是不希望有的,因此您可以通过设置output_precision来告诉它们消失:

Option 4, specify a output_precision(num)

You noticed that option 3 above kept the trailing zeros, which may be undesirable, so you can tell them to go away by setting output_precision:

mymatrix=[100.1234567, 2.12345; 3.1234567891, 4.1234];
disp(mymatrix);
output_precision(3)
disp(mymatrix)

输出:

100.1235     2.1235
  3.1235     4.1234

100.123     2.123
  3.123     4.123

当尝试进行舍入时,八度音可能会有一些非常奇怪的行为,请记住,八度尝试将均值舍入均匀地应用于矩阵中的所有项目.因此,如果您有多个具有非常不同的值的列,则八度可能会查看一个特别小的值,并认为:嘿,我应该将其转换为指数:0.0001到1.0e-04,因此,将相同的事物应用于整个矩阵.

Octave can have some really odd behavior when trying to do rounding, remember, that octave tries hard to uniformly apply rounding to all items in a matrix. So if you have multiple columns with wildly different values, octave may look at one partcularly tiny value and think: "Hey, I should convert that to exponential: 0.0001 to 1.0e-04, and thus, the same thing is applied to the entire matrix.

这篇关于GNU Octave,将数字四舍五入为单位精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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