One of the things that I spend a lot of time doing in Mathematica is creating lists of co-ordinates so that I can export them into structural analysis software, either before or after I’ve rotated and transformed them through space to mimic a deployable structure.
Most of these methods I’ve picked up along the way through trawling Stack Exchange which I find a great resource for learning Mathematica, I’m not able to link to all of them as I’ve hoovered them up into a notebook over a long period of time and not kept all of the original links…
Creating lists.
Frequently I’ll create a list of x co-ordinates, then y co-ordinates, then the z co-ordinates. There are a multitude of ways to do this, a few of the ways to create a list of co-ordinates are linked below:
1 2 3 |
x = Range[0, 6] y = Table[i, {i, 10, 16}] z = Flatten[Array[# - 1 &, 7]] |
Other ways of creating lists, could make use of functions.
1 2 |
shift[a_, b_, c_] := Table[i, {i, a, b, c}] shift[0, 10, 2] |
{0, 2, 4, 6, 8, 10}
1 2 |
f[d_] := d^2 f[{1, 2, 3, 4, 5}] |
{1, 4, 9, 16, 25}
Creating points
And there are dozens of other methods that are available, but once you have your list of x, y, and z co-0rdinates then the next step is to combine them. You could certainly type them in long hand as below, but the more nodes you have the longer it takes.
1 |
points = {{0, 0, 0}, {1, 0, 0}, {2, 0, 0}, {3, 0, 0}, {4, 0, 0}, {5, 0, 0}} |
You could automate a simple list of co-ordinates like above in a couple of ways:
1 |
Table[{i, 0, 0}, {i, 0, 5}] |
or
1 |
Array[{# - 1, 0, 0} &, 6] |
Both return the same list of co-ordinates:
{{0, 0, 0}, {1, 0, 0}, {2, 0, 0}, {3, 0, 0}, {4, 0, 0}, {5, 0, 0}}
Combining lists.
Or you might have created a list of points, the same as the lists x,y, and z at the top of this post and now want to combine them…
1 2 3 |
x = {0, 1, 2, 3, 4, 5, 6} y = {10, 11, 12, 13, 14, 15, 16} z = {0, 1, 2, 3, 4, 5, 6} |
Thread.
Thread[ ] is available and is one of the quicker methods for knitting together lists.
1 |
Thread[{x, y, z}] |
{{0, 10, 0}, {1, 11, 1}, {2, 12, 2}, {3, 13, 3}, {4, 14, 4}, {5, 15, 5}, {6, 16, 6}}
MapThread.
An alternative is MapThread[ ]
1 |
MapThread[{#1, #2, #3} &, {x, y, z}] |
or
1 |
MapThread[List, {x, y, z}] |
both return.
{{0, 10, 0}, {1, 11, 1}, {2, 12, 2}, {3, 13, 3}, {4, 14, 4}, {5, 15, 5}, {6, 16, 6}}
Transpose.
Transpose[ ] can be used for nice tidy syntax
1 |
Transpose[{x, y, z}] |
Inner.
If there is a simple 2D set of co-ordinates, then these can be combined using Inner[ ]
1 |
Inner[List, x, y, List] |
Riffle.
Again for simple 2D lists, the function Riffle[ ] can be used, but needs to be used in combination with Partition[ ]
1 |
Partition[Riffle[x, y], 2] |
If you’re working with multiple lists, then a function called multiRiffle can be written, taken from here.
1 2 |
multiRiffle[x : _List ..] := Module[{i = 1}, Fold[Riffle[##, {++i, -1, i}] &, {x}]] |
1 |
Partition[multiRiffle[x, y, z], 3] |
Gives
{{0, 10, 0}, {1, 11, 1}, {2, 12, 2}, {3, 13, 3}, {4, 14, 4}, {5, 15, 5}, {6, 16, 6}}
Custom functions.
If you only have 2D data points then a function could be written to knit them together, these functions can check to see if the lists are of the same length too which can be beneficial.
1 2 3 |
pairUp[xValues_, yValues_] := ({xValues[[#]], yValues[[#]]}) & /@ Range[Min[Length[xValues], Length[yValues]]]; pairUp[x, y] |
{{0, 10}, {1, 11}, {2, 12}, {3, 13}, {4, 14}, {5, 15}, {6, 16}}
Which can be adapted for 3D data points easily enough.
1 2 3 4 |
threeUp[xValues_, yValues_, zValues_] := ({xValues[[#]], yValues[[#]], zValues[[#]]}) & /@ Range[Min[Length[xValues], Length[yValues], Length[zValues]]]; threeUp[x, y, z] |
{{0, 10, 0}, {1, 11, 1}, {2, 12, 2}, {3, 13, 3}, {4, 14, 4}, {5, 15, 5}, {6, 16, 6}}
Hopefully this will help someone who’s learning Mathematica who’s going to be working with data points and co-ordinates a lot. It seems to be a topic that gets asked a lot on Mathematica Stack Exchange so I thought it would be helpful to try and summarise up in one post.