How do I store x,y,z co-ordinates in a table of 3 dimensions instead of 4?

I want to store the Cartesian co-ordinates of volume elements inside a finite volume box,

posn = Table[{i, j, k}, {i, x1 = -5, x2 = 5, d = .5}, {j, y1 = -5,
y2 = 5, d = .5}, {k, z1 = -5, z2 = 5, d = .5}];


I expect it to give me a three dimensional array, but on checking its dimensions, following output is obtained:

Dimensions[posn]
{21, 21, 21, 3}


How do I store the values in 3-dimensional array instead?

• But what would those dimensions correspond to? Now first three are about i j k and the deepest one is {i,j,k} itself. If you want just a 2D array n x 3 you can use Flatten] like in mathematica.stackexchange.com/q/140856/5478
– Kuba
Commented Apr 27, 2017 at 11:33
• Unless I missed something I think you are confusing posn dimensions with the length of the vector you have at the deepest level.
– Kuba
Commented Apr 27, 2017 at 11:37
• The result is a 21 * 21 * 21 array of 3 numbers, the x, y, and z coordinates. Commented Apr 27, 2017 at 11:46
• BTW, you don't need the x1=, x2=, d=, etc. parts in the Table. Commented Apr 27, 2017 at 12:38
• As a side note, you might be interested in CoordinateBoundsArray Commented Apr 27, 2017 at 12:39

Lot's of ways to skin the cat. You can fully Flatten[ ] and then re-Partition[ ] like this...

    posn = Table[{i, j, k}, {i, x1 = -5, x2 = 5, d = .5},
{j, y1 = -5, y2 = 5, d = .5}, {k, z1 = -5, z2 = 5, d = .5}];

dposn = Partition[Flatten[posn], 3];
Dimensions[dposn]


$$\{9261,3\}$$

• Just Flatten to level 2 to avoid the need to Partition: Flatten[posn, 2] Commented Apr 27, 2017 at 15:19
• @Bob Hanlon, yeah, insert mathematician joke about "reducing to known problem" in there somewhere. Commented Apr 27, 2017 at 16:45

In:

posn = Table[
ToString[{i, j, k}], {i, x1 = -5, x2 = 5, d = .5}, {j, y1 = -5,
y2 = 5, d = .5}, {k, z1 = -5, z2 = 5,
d = .5}](*store*)
Dimensions@posn
ToExpression@Part[posn, 1, 1, 1] (*access*)


Out:

{21, 21, 21}
{-5., -5., -5.}