如何在matlab中修复矩阵元素的小数位?

时间:2017-03-06 15:33:45

标签: matlab matrix rounding

我有一个订单3 x 3的矩阵,矩阵的所有元素都是小数点后6位。我想显示这个矩阵元素,最多只能小数点后5位。我使用了format shortformat long,但是它给出了4位或15位小数。 是否有一个命令可以放弃任何特定的小数位? 我知道单个数字,但无法解决矩阵的所有条目。

1 个答案:

答案 0 :(得分:3)

内置format选项无法处理此问题。您需要使用fprintfnum2str(使用format specifier)来强制显示数字

data = rand(3) * 100;
num2str(data,'%12.5f')

%   20.42155     3.95486    91.50871
%    9.28906    87.24924    72.61826
%   47.43655    95.70325    94.41092

如果你想将它作为命令行you could overload the builtin display method for double的默认显示,但我不建议这样做。

或者,您可以使用vpa指定要显示的有效位数(note that the second input is the number of significant digits and not the number of numbers after the radix point)。

vpa(data, 5)