I was wondering what is better style / more efficient:
x = linspace(-1, 1, 100);
or
x = -1:0.01:1;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As Oli Charlesworth mentioned, in
linspaceyou divide the interval[a,b]intoNpoints, whereas with the:form, you step-out fromawith a specified step size (default 1) till you reachb.One thing to keep in mind is that
linspacealways includes the end points, whereas,:form will include the second end-point, only if your step size is such that it falls on it at the last step else, it will fall short. Example:That said, when I use the two approaches depends on what I need to do. If all I need to do is sample an interval with a fixed number of points (and I don’t care about the step-size), I use
linspace.In many cases, I don’t care if it doesn’t fall on the last point, e.g., when working with polar co-ordinates, I don’t need the last point, as
2*piis the same as0. There, I use0:0.01:2*pi.