Suppose you want to create a uniform square grid of initial conditions on the (x,y) plane. This can be done using

s = 2;
step = 0.01;
data = Flatten[Table[{i, j}, {i, -s, s, step}, {j, -s, s, step}], 1];
ICs = Length[data]

Now my question is rather simple. How can I adjust the step so as the Length of the list to be equal to N, let's say N = 1000?

Many thanks in advance!

  • 2
    $\begingroup$ Not really a MMA question, to set the side length of your square grid to N, then use step = 2s/(N-1). $\endgroup$ – Quantum_Oli Dec 8 '15 at 11:15
  • 1
    $\begingroup$ But as Kuba says, you'll have a problem if you want to the the overall length of your flattened list to a non-square number. (As you'd have to use different step sizes in the i and j directions). $\endgroup$ – Quantum_Oli Dec 8 '15 at 11:16
  • 5
    $\begingroup$ 32715 38009 $\endgroup$ – Kuba Dec 8 '15 at 11:21
  • $\begingroup$ Since N@Array[ic, Sqrt[100] {1, 1}, {{-s, s}, {-s, s}}] // Flatten // Length works, then it is a duplicate. $\endgroup$ – Kuba Dec 8 '15 at 11:27
  • $\begingroup$ @Quantum_Oli I run several tests with the code proposed yesterday regarding the basins of attraction and the results seem free of obvious errors. I have in mind another type of plot and I really want your ideas on how to modify the code for this specific plot. $\endgroup$ – Vaggelis_Z Dec 8 '15 at 11:32
In[55]:= stp[len_] := NSolve[(2./step1)*2 + 1 == Sqrt[len], step1]

In[56]:= stp[160801.]

Out[56]= {{step1 -> 0.01}}

In[57]:= stp[10000]

Out[57]= {{step1 -> 0.040404}}

We have Range and Table which require specifying the step size, and not the final list length (unlike MATLAB's linspace). Version 10.1 introduces the convenience function Subdivide which takes the number of intervals n into which the input interval will be subdivided. Thus it gives n+1 points.

You can generate an n by m grid using

Tuples[{Subdivide[xmin, xmax, n-1], Subdivide[ymin, ymax, m-1]}]

This is a sort of an analog of using linspace with meshgrid in MATLAB. I had the impression that you were looking for something like that.

  • $\begingroup$ Any suggestion for those stuck in v9.0? $\endgroup$ – Vaggelis_Z Dec 8 '15 at 11:21
  • $\begingroup$ @Vaggelis_Z yep, take a look at links I've included. $\endgroup$ – Kuba Dec 8 '15 at 11:23
  • $\begingroup$ @Vaggelis_Z Subdivide is really just a convenience function which can easily be re-implemented in terms of Range. $\endgroup$ – Szabolcs Dec 8 '15 at 11:23
s = 2;
len = 100;

data =
 Table[{i, j}, {i, -s, s, (s - -s)/(len - 1)}, {j, -s, s, (s - -s)/(len - 1)}];


{100, 100, 2}


MakeGrid[{x1_, x2_}, {y1_, y2_}, {xn_, yn_}] :=
 Table[{x, y}, {x, x1, x2, (x2 - x1)/(xn - 1)}, {y, y1, y2, (y2 - y1)/(yn - 1)}]

MakeGrid[{2, 4}, {1, 3}, {50, 100}] // Dimensions

{50, 100, 2}


Not the answer you're looking for? Browse other questions tagged or ask your own question.