在Matlab矩阵中设置元素的精度 [英] Set precision an element in Matlab matrix
问题描述
我想为矩阵的所有元素设置精度.以下是我的工作:
I want to set precision for all element of a matrix. Below is what I did:
>>A
A =
0 1.0000 0 0 0 0
-137.0830 0 0 0 0 0
0 0 0 1.0000 0 0
365.5546 0 0 0 0 0
0 0 0 0 0 1.0000
365.5546 0 0 0 0 0
>> vpa(A,2)
ans =
[ 0, 1.0, 0, 0, 0, 0]
[ -144.0, 0, 0, 0, 0, 0]
[ 0, 0, 0, 1.0, 0, 0]
[ 377.0, 0, 0, 0, 0, 0]
[ 0, 0, 0, 0, 0, 1.0]
[ 377.0, 0, 0, 0, 0, 0]
结果不是我的愿望,应该是: -第一列中的-137.08、365.55、365.55. 请帮助建议我如何获得它. 非常感谢!
The result is not my desire, it should be: -137.08, 365.55, 365.55 in the first column. Please help to suggest me how to get it. Thank you so much!
推荐答案
您没有正确使用vpa
.从文档:
vpa(x,d)使用至少d个有效数字
vpa(x,d) uses at least d significant digits
注意至少说 .这就是为什么当您只要求输入2个有效数字时仍然得到377的原因.
Nota that it says at least. That is why you still get 377 when you only asked for 2 significant digits.
您似乎不知道有效数字是多少.来自维基百科:
It seems that you don't know what significant digits are. From Wikipedia:
数字的有效数字是带有含义的数字 有助于其测量分辨率.这包括所有数字 除了:
The significant figures of a number are digits that carry meaning contributing to its measurement resolution. This includes all digits except:
- 所有前导零;
- 在零仅用作占位符时指示数字的刻度(精确规则在识别时说明) 重要数字);和
- 例如通过比原始数据或测量更高的精度进行计算而引入的虚假数字 报告的精度要比设备支持的精度高.
- All leading zeros;
- Trailing zeros when they are merely placeholders to indicate the scale of the number (exact rules are explained at identifying significant figures); and
- Spurious digits introduced, for example, by calculations carried out to greater precision than that of the original data, or measurements reported to a greater precision than the equipment supports.
所以你想要这个
>> vpa(365.5546, 5)
ans =
365.55
Now, to be consistent, you need to find out what is the maximum of your matrix and compute the desired number of significant digits from there.
max_number = floor(log10(max(abs(A(:))+1)) + 1);
decimals = 2;
vpa(A, max_number + decimals)
在这里,max_number
是矩阵具有的整数位的最大位数,而decimals
是要具有的小数位数.
Here, max_number
is the maximum number of integer digits that your matrix has, and decimals
is the number of decimal places that you want to have.
这篇关于在Matlab矩阵中设置元素的精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!