Generate a numerical series

1 view (last 30 days)
Vittorio Faustinella
Vittorio Faustinella on 28 Jan 2022
Edited: John D'Errico on 28 Jan 2022
Hi everyone, I want to generate this vector: 1e-8, 1e-9, 1e-10, 1e-11, 1e-12, 1e-13, 1e-14.
How can i do it?
Thanks

Accepted Answer

DGM
DGM on 28 Jan 2022
Edited: DGM on 28 Jan 2022
This is one way:
k = -8:-1:-14;
v = 10.^k
v = 1×7
1.0e+-8 * 1.0000 0.1000 0.0100 0.0010 0.0001 0.0000 0.0000
fprintf('%g\n',v.') % for sake of clarity
1e-08 1e-09 1e-10 1e-11 1e-12 1e-13 1e-14

More Answers (1)

John D'Errico
John D'Errico on 28 Jan 2022
Edited: John D'Errico on 28 Jan 2022
While @DGM is correct (+1 from me on that answer, please accept that answer, as it is the correct one), in that it is what I would do immediately, there are always many ways to do things like this. The alternative I can think of immediately is to use cumprod with repmat:
format long g
v = cumprod([1e-8,repmat(0.1,1,6)])
v = 1×7
1.0e+00 * 1e-08 1e-09 1e-10 1e-11 1e-12 1e-13 1e-14
fprintf('%g\n',v.') % for sake of clarity
1e-08 1e-09 1e-10 1e-11 1e-12 1e-13 1e-14
I could also have used cumprod, in conjunction with repelem instead of repmat. That would be slightly less kludgy looking.
cumprod([1e-8,repelem(0.1,6)])'
ans = 7×1
1.0e+00 * 1e-08 1e-09 1e-10 1e-11 1e-12 1e-13 1e-14
Then I think of logspace, which does work nicely. In fact, logspace is a pretty clean looking solution. Still not as obvious in my eyes as that which DGM proposed.
logspace(-8,-14,7).'
ans = 7×1
1.0e+00 * 1e-08 1e-09 1e-10 1e-11 1e-12 1e-13 1e-14
And of course, there is this devious solution:
exp((-8:-1:-14)*log(10)).'
ans = 7×1
1.0e+00 * 9.99999999999998e-09 9.99999999999997e-10 9.99999999999996e-11 9.99999999999998e-12 9.99999999999997e-13 9.99999999999996e-14 9.99999999999999e-15
which is technically correct, exceot that floating point trash appears in the least significant bits. But 9.99999999999999e-9 is technically 1e-8, exept for an error in the least significant bit of the result.
I can stop here, but with some effort, I'd bet there are more.

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!