1

I have three tables X,Y,Z. While X & Y define my grid points the Z depends on every point of X and Y.

x = Table[i, {i, 0, 10, 1}]
y = Table[j, {j, 0, 10, 1}]
z = Table[5*i + j, {i, 0, 10, 1}, {j, 0, 10, 1}]

Now I want the final list to look like this [{x1,y1,z1},{x2,y2,z2}}

I want to create a set of corresponding x,y,z values from the table given above.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
fazil
  • 47
  • 1
  • 2
  • possible duplicate of [Mathematica Lists Combining X,Y,Z Co-Ordinates](http://stackoverflow.com/questions/7065618/mathematica-lists-combining-x-y-z-co-ordinates) – rcollyer Aug 15 '11 at 15:53
  • It appears, I can't rescind my close vote, but there is an alternate solution here that isn't simply available to the prior question. – rcollyer Aug 15 '11 at 15:55
  • @fazil You doesn't have to create a new identity for every question you post. Something went wrong? – Sjoerd C. de Vries Aug 15 '11 at 16:11
  • 1
    @Sjoerd, he's unregistered, and I believe that restricts you to one question. On the other hand, if he does register, he may request that the various accounts are merged, so he gets credit for and the ability to manipulate all his prior posts. – rcollyer Aug 15 '11 at 17:22
  • 1
    May I suggest refraining from using language such as "HOW DO I DO IT???" and "PLEASE HELP" in your questions. It's not a great way to attract help. – Kev Aug 15 '11 at 17:36
  • 1
    @rcollyer I don't think its really a duplicate, it's just similar – acl Aug 15 '11 at 18:37
  • @acl, hence my second comment and my desire to rescind my close vote. – rcollyer Aug 15 '11 at 18:43
  • 1
    @rcollyer I requested a merger. Seems to have been done already. – Sjoerd C. de Vries Aug 15 '11 at 19:07

3 Answers3

6

In this case you can also produce your combined list with Array as follows:

Array[{##, 5 # + #2} &, {11, 11}, 0]

See Function and Slot. rcollyer has already shown how to "split out" x, y, and z from this.

When starting with unrelated lists x and y you can produce the combined list with Outer:

Outer[{##, 5 # + #2} &, x, y, 1]
Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
4

Unless you need the the x and y lists, I'd combine this in one Table as follows:

Table[{i, j, 5*i + j}, {i, 0, 10}, {j, 0, 10}]

Note, I removed the step length ({i, 0, 10, 1} -> {i, 0, 10}) as it's implicitly set to 1 if it is not included.

Edit: If you wish to have the x and y lists, also, you could do the following

Table[{i, j, 5*i+j}, {i, x}, {j, y}]

As of v.7, Table accepts lists of values in addition to start and end points. This doesn't address whether you need a separate list for z, also. In that case, I'd start with the first form bit of code, and using Transpose (per your other question) to set the individual lists, as follows:

coords = Table[{i, j, 5*i + j}, {i, 0, 10}, {j, 0, 10}];
{x, y, z} = Transpose @ coords;
Community
  • 1
  • 1
rcollyer
  • 10,475
  • 4
  • 48
  • 75
2

One way to do it starting from your

x = Table[i, {i, 0, 10, 1}];
y = Table[j, {j, 0, 10, 1}];
z = Table[5*i + j, {i, 0, 10, 1}, {j, 0, 10, 1}];

is

Flatten[
    MapThread[{Sequence @@ #1, #2} &,
        {Outer[{#1, #2} &, x, y], z},
        2
    ],
    1
]

(I'd love to see me try to understand this in a week) which gives what you want.

This also works:

p = {};
Do[
    Do[
        AppendTo[p, {x[[i]], y[[j]], z[[i, j]]}],
        {j, 1, Length@y}
    ],
    {i, 1, Length@x}
]

and gives the same answer.

acl
  • 6,490
  • 1
  • 27
  • 33