1
$\begingroup$

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?

$\endgroup$
6
  • $\begingroup$ 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 $\endgroup$
    – Kuba
    Commented Apr 27, 2017 at 11:33
  • 2
    $\begingroup$ Unless I missed something I think you are confusing posn dimensions with the length of the vector you have at the deepest level. $\endgroup$
    – Kuba
    Commented Apr 27, 2017 at 11:37
  • 1
    $\begingroup$ The result is a 21 * 21 * 21 array of 3 numbers, the x, y, and z coordinates. $\endgroup$
    – evanb
    Commented Apr 27, 2017 at 11:46
  • 2
    $\begingroup$ BTW, you don't need the x1=, x2=, d=, etc. parts in the Table. $\endgroup$
    – Chris K
    Commented Apr 27, 2017 at 12:38
  • 1
    $\begingroup$ As a side note, you might be interested in CoordinateBoundsArray $\endgroup$
    – Michael E2
    Commented Apr 27, 2017 at 12:39

2 Answers 2

1
$\begingroup$

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\}$$

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

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.}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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