MATLAB 中的科学记数法
Scientific notation in MATLAB
假设我有一个包含以下元素的数组:
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 *
(底数和指数)?
如果我这样做 arr(10)
,那么这将 return 9.0120e+13
而不是 0.9012e+14
.
假设题目是抓取数组中系数小于1的任意元素。有没有办法获得1.0e+14
,这样我就可以做到arr(i) < 1.0e+14
?
Here是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".
现在你有了科学记数法的指数。这应该能让你达到你的目标 ;-).
并且根据您要对指数和数字执行的操作,您还可以定义自己的方法。 this 线程中描述了一个示例。
我假设你想要字符串输出。
让 a
表示输入数值数组。如果您不介意使用 evalc
(eval
的变体,这被认为是不好的做法,您可以这样做:
s = evalc('disp(a)');
s = regexp(s, '[\de+-\.]+', 'match');
这会生成一个包含所需字符串的元胞数组。
示例:
>> 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'
假设我有一个包含以下元素的数组:
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 *
(底数和指数)?
如果我这样做 arr(10)
,那么这将 return 9.0120e+13
而不是 0.9012e+14
.
假设题目是抓取数组中系数小于1的任意元素。有没有办法获得1.0e+14
,这样我就可以做到arr(i) < 1.0e+14
?
Here是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".
现在你有了科学记数法的指数。这应该能让你达到你的目标 ;-).
并且根据您要对指数和数字执行的操作,您还可以定义自己的方法。 this 线程中描述了一个示例。
我假设你想要字符串输出。
让 a
表示输入数值数组。如果您不介意使用 evalc
(eval
的变体,这被认为是不好的做法,您可以这样做:
s = evalc('disp(a)');
s = regexp(s, '[\de+-\.]+', 'match');
这会生成一个包含所需字符串的元胞数组。
示例:
>> 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'