0

Say I have an array that contains the following elements: 1.0e+14 * 1.3325 1.6485 2.0402 1.0485 1.2027 2.0615 1.7432 1.9709 1.4807 0.9012

Now, is there a way to grab 1.0e+14 * (base and exponent) individually? If I do arr(10), then this will return 9.0120e+13 instead of 0.9012e+14.

Assuming the question is to grab any elements in the array with coefficient less than one. Is there a way to obtain 1.0e+14, so that I could just do arr(i) < 1.0e+14?

firefly
  • 201
  • 1
  • 3
  • 11
  • The answer depends on what you want to do with the result. For example, should it be a string or a number? Please edit your question to include some more details about the context. – Matt Jul 19 '15 at 18:44
  • Basically, when retrieving any element from the array, I want them to have the same base and exponent. So, for example, `0.9012e+14` instead of `9.0120e+13`. – firefly Jul 19 '15 at 18:47
  • 1
    So you want a _string_ as output, right? – Luis Mendo Jul 19 '15 at 18:49
  • I think he wants to *force* a given base. @LuisMendo – Matt Jul 19 '15 at 18:50
  • OK.. maybe I should of state the problem more clearly. Say I want to compare the values in the given array, and I'm trying to find the elements with coefficient less than 1. That's why I want 0.9012e+14 instead of 9.0120e+13. – firefly Jul 19 '15 at 18:54
  • 1
    Unless you want _strings_, saying "0.9012e+14 instead of 9.0120e+13" doesn't make sense. _Numerically_, 0.9012e+14 and 9.0120e+13 are the same – Luis Mendo Jul 19 '15 at 19:01

2 Answers2

1

I assume you want string output.

Let a denote the input numeric array. You can do it this way, if you don't mind using evalc (a variant of eval, which is considered bad practice):

s = evalc('disp(a)');
s = regexp(s, '[\de+-\.]+', 'match');

This produces a cell array with the desired strings.

Example:

>> a = [1.2e-5 3.4e-6]
a =
   1.0e-04 *
    0.1200    0.0340
>> s = evalc('disp(a)');
>> s = regexp(s, '[\de+-\.]+', 'match')
s = 
    '1.0e-04'    '0.1200'    '0.0340'
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • First of all, thank you very much for your help. Though, I wanted integer output, but I think this is one way to solve the problem. – firefly Jul 19 '15 at 19:09
-1

Here is the original answer from Alain.

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

Now you have the exponent of the scientific notation. This should allow you to get to whatever your goal is ;-).

And depending on what you want to do with your exponent and the number, you could also define your own method. An example is described in this thread.

Community
  • 1
  • 1
eschanet
  • 1,063
  • 6
  • 15
  • 2
    This is a copy of http://stackoverflow.com/questions/10471031/how-to-get-exponent-of-scientific-notation-in-matlab without attribution! – Matt Jul 19 '15 at 18:49