简体   繁体   English

如何在Matlab中得到科学记数的指数

[英]How to get Exponent of Scientific Notation in Matlab

When the numbers are really small, Matlab automatically shows them formatted in Scientific Notation. 当数字非常小时,Matlab会自动将它们格式化为科学记数法。

Example: 例:

A = rand(3) / 10000000000000000;

A =

  1.0e-016 *

    0.6340    0.1077    0.6477
    0.3012    0.7984    0.0551
    0.5830    0.8751    0.9386

Is there some in-built function which returns the exponent? 是否有一些内置函数返回指数? Something like: getExponent(A) = -16 ? 像: getExponent(A) = -16

I know this is sort of a stupid question, but I need to check hundreds of matrices and I can't seem to figure it out. 我知道这是一个愚蠢的问题,但我需要检查数百个矩阵,我似乎无法弄明白。

Thank you for your help. 谢谢您的帮助。

Basic math can tell you that: 基础数学可以告诉你:

floor(log10(N))

The log base 10 of a number tells you approximately how many digits before the decimal are in that number. 数字的对数基数10告诉您大约在该数字前面的小数位数。

For instance, 99987123459823754 is 9.998E+016 例如, 999871234598237549.998E+016

log10(99987123459823754) is 16.9999441 , the floor of which is 16 - which can basically tell you "the exponent in scientific notation is 16, very close to being 17". log10(99987123459823754)16.9999441 ,其底部是16 - 它基本上可以告诉你“科学记数法中的指数是16,非常接近17”。

Floor always rounds down, so you don't need to worry about small exponents: 地板总是向下舍入,所以你不必担心小指数:

0.000000000003754 = 3.754E-012
log10(0.000000000003754) = -11.425
floor(log10(0.000000000003754)) = -12

You can use log10(A) . 您可以使用log10(A) The exponent used to print out will be the largest magnitude exponent in A. If you only care about small numbers (< 1), you can use 用于打印输出的指数将是A中最大的指数。如果你只关心小数(<1),你可以使用

min(floor(log10(A)))

but if it is possible for them to be large too, you'd want something like: 但如果它们也可能很大,你会想要这样的东西:

a = log10(A);
[v i] = max(ceil(abs(a)));
exponent = v * sign(a(i));

this finds the maximum absolute exponent, and returns that. 这将找到最大绝对指数,并返回该指数。 So if A = [1e-6 1e20] , it will return 20. 因此,如果A = [1e-6 1e20] ,它将返回20。

I'm actually not sure quite how Matlab decides what exponent to use when printing out. 我实际上不确定Matlab如何确定打印时使用的指数。 Obviously, if A is close to 1 (eg A = [100, 203] ) then it won't use an exponent at all but this solution will return 2. You'd have to play around with it a bit to work out exactly what the rules for printing matrices are. 显然,如果A接近于1(例如A = [100, 203] ),那么它根本不会使用指数,但是这个解决方案将返回2.你必须稍微使用它才能确切地计算出来打印矩阵的规则是什么。

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

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