开发者

Mathematica Tables sets

开发者 https://www.devze.com 2023-03-28 18:20 出处:网络
I have three tables X,Y,Z. While X & Y define my grid points the Z depends on every point of X and Y.

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.


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]


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;


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消