简体   繁体   English

MATLAB中的科学计数法

[英]Scientific notation in MATLAB

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 假设我有一个包含以下元素的数组: 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? 现在,有没有办法单独获取1.0e+14 * (基数和指数)? If I do arr(10) , then this will return 9.0120e+13 instead of 0.9012e+14 . 如果我做arr(10) ,那么它将返回9.0120e+13而不是0.9012e+14

Assuming the question is to grab any elements in the array with coefficient less than one. 假设问题在于获取系数小于1的数组中的任何元素。 Is there a way to obtain 1.0e+14 , so that I could just do arr(i) < 1.0e+14 ? 有没有一种方法可以获取1.0e+14 ,因此我可以做arr(i) < 1.0e+14

I assume you want string output. 我假设您要输出字符串。

Let a denote the input numeric array. a表示输入数字数组。 You can do it this way, if you don't mind using evalc (a variant of eval , which is considered bad practice): 如果您不介意使用evalceval一种变体,被认为是不好的做法),则可以通过这种方式进行操作:

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'

Here is the original answer from Alain. 是阿兰的原始答案。

Basic math can tell you that: 基本数学可以告诉您:

floor(log10(N)) 地板(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 例如,99987123459823754是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". log10(99987123459823754)是16.9999441,其下限是16-这基本上可以告诉您“科学计数法的指数是16,非常接近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. 线程中描述了一个示例。

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

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