Python Python 3.7.4 lacks a proper linspace function?

  • Thread starter Thread starter Wrichik Basu
  • Start date Start date
  • Tags Tags
    Function Python
Click For Summary
Python's Numpy library provides a linspace function for generating linearly spaced arrays, but users seek a simpler syntax similar to Matlab's colon operator. The example given demonstrates how to create an array of integers between 0 and 3 with a specified increment using Numpy. While Numpy's linspace can produce similar results, it requires specifying the number of points, which can be less intuitive. The discussion highlights that including the endpoint in the linspace function can be managed by adjusting the parameters. Overall, while Numpy offers functionality, users desire more straightforward alternatives akin to Matlab's syntax.
Wrichik Basu
Science Advisor
Insights Author
Gold Member
Messages
2,180
Reaction score
2,721
Basically, I wanted to create a Numpy array with linearly spaced integers between 0 and 3, the increment being 0.01.

Yes, I know Numpy offers a linspace function. I used it like this: x = np.linspace(0, 3, num=300) (where np is numpy), and got this:

1566501564585.png


I know that the numbers cannot be exact (can you explain why?). Matlab returns something similar when the linspace function is used.

But then, in Matlab, I can write x = 0:0.01:3, which will give me this:

1566501758159.png


Does Python have something similar?
 
Technology news on Phys.org
Have you read the docs on this function?

It seems like the only difference is between
Python:
import numpy as np

np.linspace(0, 3,300, False)
np.linspace(0, 3,300, True)
 
  • Like
Likes Wrichik Basu
Another way is to include the endpoint, but increase num to 301. Just found this, btw.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K