2

I would like to create an array with the form [1, 2, 4, 8, 16, 32, ...]. Not always with that spacing between numbers.

Is there a linspace/arange type function which allows me to create vector of non equally spaced numbers?

Thanks

6
  • 2
    In your case, seems like you want [2]**np.arange(6) Commented Aug 23, 2018 at 19:59
  • I think you need to better define your requirements. "non-equally spaced numbers" is very vague. @RafaelC offers a solid solution. But you way have further requirements we are not aware of. Commented Aug 23, 2018 at 20:01
  • You need specifically a python (numpy) function? AAny problem with this way: [2**i for i in range(6)] Commented Aug 23, 2018 at 20:03
  • @rahlf23 It is vague on purpose. I'm looking for a flexible function (if it exist). And as I stated in my question, it will not always have the same spacing. Commented Aug 23, 2018 at 20:06
  • @Hamidreza I'm aware that I can build my own function, nut I'm new to numpy and I want to know if there's a predefined tool for this. Maybe the answer is that there's none and that I must build my own. Commented Aug 23, 2018 at 20:08

1 Answer 1

5

Yes, there is also logspace and geomspace in NumPy.

np.geomspace(1,32,6) gives you array([ 1., 2., 4., 8., 16., 32.])

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.