简体   繁体   中英

How to get Exponent of Scientific Notation in Matlab

When the numbers are really small, Matlab automatically shows them formatted in Scientific Notation.

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 ?

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.

For instance, 99987123459823754 is 9.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".

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) . 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

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.

I'm actually not sure quite how Matlab decides what exponent to use when printing out. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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