Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 419
Rachel Avatar asked May 06 '12 13:05

Rachel


People also ask

How does matlab calculate scientific notation?

For very large numbers or very small numbers it is more convenient to express the number in scientific notation using a power of 10. For example, 1,400,000 can be written as 1.4 x 106. In MATLAB (and other programming languages) a convenient shorthand for this is 1.4e6. The diameter of a carbon nanotube is 5.2nm.

How do you print an exponential notation in matlab?

sprintf('%d',x) prints out exponential notation instead of decimal notation.

How do I get rid of e 01 in matlab?

you can use "format short g" command in the start of the code. I am a fresher in matlab but as far as i know it can help to get rid of e in the answer.


2 Answers

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
like image 184
Alain Avatar answered Nov 07 '22 16:11

Alain


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 (e.g. 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.

like image 27
Richante Avatar answered Nov 07 '22 15:11

Richante