简体   繁体   English

如何在MATLAB中用小数点后1位格式化科学记数法?

[英]How to format scientific notation with 1 digit after the decimal place in MATLAB?

I have tried sprintf("%.1e", x) but that gives me 6.3000e-16 .我试过sprintf("%.1e", x)但这给了我6.3000e-16 How can I cut off the zeros and display just 6.3e-16 ?如何切断零并只显示6.3e-16 I am also displaying the numbers in a table.我也在表格中显示数字。

EDIT: So now it is correctly display some numbers, but others aren't displayed in scientific notation at all.编辑:所以现在它正确显示了一些数字,但其他数字根本没有以科学记数法显示。 I am using R2018a.我正在使用 R2018a。

Here is the code I am using这是我正在使用的代码

n       = zeros(19,1);
a       = zeros(19,1);
b       = zeros(19,1);
c       = zeros(19,1);
d       = zeros(19,1);

format short
for i = 2:20
    w = 1/(2000 * i);
    x = 1/(1000 * i);
    y = 1/(50 * i);
    z = 1/(20 * i);
    n(i-1)          = sprintf("%d", i);
    a(i-1)          = sprintf("%.1e", w);
    b(i-1)          = sprintf("%.1e", x);
    c(i-1)          = sprintf("%.1e", y);
    d(i-1)          = sprintf("%.1e", z);
end

table(  n, a, b, c, d )

and here is the output:这是输出:

  19×5 table
    n        a          b         c         d   
    __    _______    _______    ______    ______
     2    0.00025     0.0005      0.01     0.025
     3    0.00017    0.00033    0.0067     0.017
     4    0.00013    0.00025     0.005     0.013
     5     0.0001     0.0002     0.004      0.01
     6    8.3e-05    0.00017    0.0033    0.0083
     7    7.1e-05    0.00014    0.0029    0.0071
     8    6.3e-05    0.00013    0.0025    0.0063
     9    5.6e-05    0.00011    0.0022    0.0056
    10      5e-05     0.0001     0.002     0.005
    11    4.5e-05    9.1e-05    0.0018    0.0045
    12    4.2e-05    8.3e-05    0.0017    0.0042
    13    3.8e-05    7.7e-05    0.0015    0.0038
    14    3.6e-05    7.1e-05    0.0014    0.0036
    15    3.3e-05    6.7e-05    0.0013    0.0033
    16    3.1e-05    6.3e-05    0.0013    0.0031
    17    2.9e-05    5.9e-05    0.0012    0.0029
    18    2.8e-05    5.6e-05    0.0011    0.0028
    19    2.6e-05    5.3e-05    0.0011    0.0026
    20    2.5e-05      5e-05     0.001    0.0025

In your code, you are assigning a string to a double-float array.在您的代码中,您将一个字符串分配给一个双浮点数组。 It looks like MATLAB automatically converts the string to a double to store it there.看起来 MATLAB 会自动将字符串转换为双精度值以将其存储在那里。 Thus, your formatting gets lost:因此,您的格式会丢失:

>> sprintf("%.1e", 1/1000)
ans = 
    "1.0e-03"
>> a=0;
>> a(1) = sprintf("%.1e", 1/1000)
a =
   1.0000e-03
>> class(a)
ans =
    'double'

Instead, use a string array:相反,使用字符串数组:

a = strings(19,1);
%...
   a(i-1) = sprintf("%.1e", w);

I'm not used to the new strings, and this behavior surprises me.我不习惯新的字符串,这种行为让我感到惊讶。 Assigning a number to a string converts the number to a string, and assigning the string to a number converts it back to a number.将数字分配给字符串会将数字转换为字符串,将字符串分配给数字会将其转换回数字。 This does not happen with the "old-fashioned" char arrays: “老式”字符数组不会发生这种情况:

>> a=0;
>> a(1) = sprintf('%.1e', 1/1000);
Unable to perform assignment because the left and right sides have a different number of elements.

When using char arrays, store them in a cell array:使用 char 数组时,将它们存储在元胞数组中:

a = cell(19,1);
%...
   a{i-1} = sprintf('%.1e', w);

You can use "fprintf":您可以使用“fprintf”:

>> fprintf("%.1f\n", pi)
3.1

To show more or less digits just adjust the number after the dot.要显示更多或更少的数字,只需调整点后的数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM