Mathematica 表格集

发布于 2024-11-29 12:48:08 字数 373 浏览 3 评论 0原文

我有三个表X,Y,Z。而X & Y 定义我的网格点,Z 取决于 XY 的每个点。

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}]

现在我希望最终列表看起来像这样 [{x1,y1,z1},{x2,y2,z2}}

我想创建一组相应的 x,y,z 上面给出的表中的值。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月亮是我掰弯的 2024-12-06 12:48:08

在这种情况下,您还可以使用 Array 如下:

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

参见 Function插槽。 rcollyer 已经展示了如何从中“拆分”x、y 和 z。

当从不相关的列表 xy 开始时,您可以使用 外部

Outer[{##, 5 # + #2} &, x, y, 1]

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]
如若梦似彩虹 2024-12-06 12:48:08

除非您需要 xy 列表,否则我会将其组合在一个 Table 中,如下所示:

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

注意,我删除了步长( {i, 0, 10, 1} -> {i, 0, 10}),因为如果不包含它,它会隐式设置为 1。

编辑:如果您还希望拥有 xy 列表,您可以执行以下操作

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

从 v.7 开始,Table 除了起点和终点之外还接受值列表。这并没有解决您是否还需要单独的 z 列表。在这种情况下,我将从第一个形式的代码开始,并使用 Transpose (根据您的其他 问题)设置各个列表,如下所示:

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

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;
打小就很酷 2024-12-06 12:48:08

从你开始的一种方法

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}];

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

(我很乐意看到我在一周内尝试理解这一点),它给出了你想要的东西。

这也有效:

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

并给出相同的答案。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文