Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linspace vs range

Tags:

vector

matlab

I was wondering what is better style / more efficient:

x = linspace(-1, 1, 100);

or

x = -1:0.01:1;
like image 621
Samuel Avatar asked Apr 25 '11 14:04

Samuel


People also ask

What is the difference between Linspace and arange?

linspace is rather similar to the np. arange function. The essential difference between NumPy linspace and NumPy arange is that linspace enables you to control the precise end value, whereas arange gives you more direct control over the increments between values in the sequence.

What is the purpose of Linspace?

Go to function: The linspace function generates linearly spaced vectors. It is similar to the colon operator ":", but gives direct control over the number of points. y = linspace(a,b) generates a row vector y of 100 points linearly spaced between a and b.

What is the difference between Linspace and in Matlab?

it is the same, the main difference and advantage of linspace is that it generates a vector of integers with the desired length (or default 100) and scales it afterwards to the desired range.

What is the difference between the Linspace command and the colon operator?

Answers (1) The linspace function produces a vector of fixed length (defined by the third argument) between the limits defined by the first two. The function determines the step size. The colon operator defines a vector from the start, step, and end values, and as the result the length is determined by those values.


1 Answers

As Oli Charlesworth mentioned, in linspace you divide the interval [a,b] into N points, whereas with the : form, you step-out from a with a specified step size (default 1) till you reach b.

One thing to keep in mind is that linspace always 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:

0:3:10

ans = 

     0     3     6     9

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*pi is the same as 0. There, I use 0:0.01:2*pi.

like image 127
abcd Avatar answered Sep 30 '22 01:09

abcd