# Calibrating the length of a list [duplicate]

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?

• Not really a MMA question, to set the side length of your square grid to N, then use step = 2s/(N-1). – Quantum_Oli Dec 8 '15 at 11:15
• 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). – Quantum_Oli Dec 8 '15 at 11:16
• – Kuba Dec 8 '15 at 11:21
• Since N@Array[ic, Sqrt {1, 1}, {{-s, s}, {-s, s}}] // Flatten // Length works, then it is a duplicate. – Kuba Dec 8 '15 at 11:27
• @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. – Vaggelis_Z Dec 8 '15 at 11:32

In:= stp[len_] := NSolve[(2./step1)*2 + 1 == Sqrt[len], step1]

In:= stp[160801.]

Out= {{step1 -> 0.01}}

In:= stp

Out= {{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.

• Any suggestion for those stuck in v9.0? – Vaggelis_Z Dec 8 '15 at 11:21
• @Vaggelis_Z yep, take a look at links I've included. – Kuba Dec 8 '15 at 11:23
• @Vaggelis_Z Subdivide is really just a convenience function which can easily be re-implemented in terms of Range. – 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)}];

Dimensions@data


{100, 100, 2}

Generalization

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}