Message Boards Message Boards

GROUPS:

[✓] Use a Wolfram Language equivalent to Matlab "linspace" function?

Posted 4 months ago
1085 Views
|
3 Replies
|
4 Total Likes
|

Hello

Matlab has a built in function called linspace, that, according to the documentation, generates a linearly spaced vector, but "gives direct control over the number of points and always includes the endpoints".

Mathematica doesn't work with vectors, rather with lists, but I wish to generate a list of numbers that start from x1 and ends at x2, with n points in between (such that the spacing between the points is (x2-x1)/(n-1).

I couldn't find help with Table. Table generates a list of numbers with a starting point and an end point, with an extra option for the intervals, but I want to have n numbers in between.

Is there any work around?

3 Replies

use:

Subdivide
Posted 4 months ago

I think what you want is Subdivide, which I believe is similar to linspace, however instead of n evenly spaced points, Subdivide generates the list of valued from subdividing the range into n equal parts. So linspace(-5,5,7) from Matlab would be equivalently N@Subdivide[-5, 5, 6] in Mathematica:

In[4]:= N@Subdivide[-5, 5, 6]
Out[4]= {-5., -3.33333, -1.66667, 0., 1.66667, 3.33333, 5.}

Note that I am not a Matlab user and haven't actually used linspace, this is just based on what I was able to infer from their documentation.

Posted 4 months ago

Thanks very much!

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract