设置精度 Matlab 矩阵中的一个元素
Set precision an element in Matlab matrix
我想为矩阵的所有元素设置精度。以下是我所做的:
>>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。
请帮助建议我如何获得它。
非常感谢!
second input to vpa
is the number of significant digits与小数点后的数值个数不相同。数字 -137.08
实际上有 五个 有效数字,因此您需要使用 5
的第二个输入
vpa(A, 5)
% [ 0, 1.0, 0, 0, 0, 0]
% [ -137.08, 0, 0, 0, 0, 0]
% [ 0, 0, 0, 1.0, 0, 0]
% [ 365.55, 0, 0, 0, 0, 0]
% [ 0, 0, 0, 0, 0, 1.0]
% [ 365.55, 0, 0, 0, 0, 0]
您没有正确使用 vpa
。来自 docs:
vpa(x,d) uses at least d significant digits
请注意,它说 至少。这就是为什么当你只要求 2 位有效数字时你仍然得到 377 的原因。
看来你不知道什么是有效数字。来自 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
现在,为了保持一致,您需要找出矩阵的最大值,然后从那里 compute the desired number of significant digits。
max_number = floor(log10(max(abs(A(:))+1)) + 1);
decimals = 2;
vpa(A, max_number + decimals)
这里,max_number
是你的矩阵的最大整数位数,decimals
是你想要的小数位数有。
vpa(x,d)
:
d
是有效数字,不是小数位。
pvalue = vpa(-137.0830);
twopvalue = vpa(-137.0830,2);
% pvalue = -137.08299999999999840838427189738
% twopvalue = -144.0
如果你想得到第一列的-137.08、365.55、365.55。您可以使用 roundn(A,-2)
roundn(A,-2)
ans =
0 1.0000 0 0 0 0
-137.0800 0 0 0 0 0
0 0 0 1.0000 0 0
365.5500 0 0 0 0 0
0 0 0 0 0 1.0000
365.5500 0 0 0 0 0
我想为矩阵的所有元素设置精度。以下是我所做的:
>>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。 请帮助建议我如何获得它。 非常感谢!
second input to vpa
is the number of significant digits与小数点后的数值个数不相同。数字 -137.08
实际上有 五个 有效数字,因此您需要使用 5
vpa(A, 5)
% [ 0, 1.0, 0, 0, 0, 0]
% [ -137.08, 0, 0, 0, 0, 0]
% [ 0, 0, 0, 1.0, 0, 0]
% [ 365.55, 0, 0, 0, 0, 0]
% [ 0, 0, 0, 0, 0, 1.0]
% [ 365.55, 0, 0, 0, 0, 0]
您没有正确使用 vpa
。来自 docs:
vpa(x,d) uses at least d significant digits
请注意,它说 至少。这就是为什么当你只要求 2 位有效数字时你仍然得到 377 的原因。
看来你不知道什么是有效数字。来自 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
现在,为了保持一致,您需要找出矩阵的最大值,然后从那里 compute the desired number of significant digits。
max_number = floor(log10(max(abs(A(:))+1)) + 1);
decimals = 2;
vpa(A, max_number + decimals)
这里,max_number
是你的矩阵的最大整数位数,decimals
是你想要的小数位数有。
vpa(x,d)
:
d
是有效数字,不是小数位。
pvalue = vpa(-137.0830);
twopvalue = vpa(-137.0830,2);
% pvalue = -137.08299999999999840838427189738
% twopvalue = -144.0
如果你想得到第一列的-137.08、365.55、365.55。您可以使用 roundn(A,-2)
roundn(A,-2)
ans =
0 1.0000 0 0 0 0
-137.0800 0 0 0 0 0
0 0 0 1.0000 0 0
365.5500 0 0 0 0 0
0 0 0 0 0 1.0000
365.5500 0 0 0 0 0