Mathematica 列出组合 X、Y、Z 坐标

发布于 2024-11-30 01:29:43 字数 321 浏览 6 评论 0原文

我是 Mathematica 的初学者。我的问题是:我在名为 XCORD、YCORD、ZCORD 的单独列表中有大量的 x、y 和 z 坐标,我想将它们组合在一个列表中

示例:
如果 x 坐标列表由 XCORD = {x1,x2,x3} 给出,则 y 坐标列表由 YCORD = {y1,y2,y3} 给出以及 ZCORD = {z1,z2,z3} 的 z 坐标列表,我想要一个如下所示的坐标结果列表:

 {{x1,y1,z1},{x2,y2,z2},{x3,y3,z3}}

I am a beginner in Mathematica. My question is: I have huge amount of x,y and z co-ordinates in separate lists named XCORD,YCORD,ZCORD and I want to combine them in one list

Example:
If the x co-oridinates list is given by XCORD = {x1,x2,x3}, the y co-ordinates list byYCORD = {y1,y2,y3} and the z co-ordinates list by ZCORD = {z1,z2,z3}, I would like to have a resulting list of co-ordinates that looks like this:

 {{x1,y1,z1},{x2,y2,z2},{x3,y3,z3}}

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

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

发布评论

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

评论(2

思念满溢 2024-12-07 01:29:43

您可以使用转置来完成此操作:

XCORD = {x1, x2, x3};
YCORD = {y1, y2, y3};
ZCORD = {z1, z2, z3};

res = Transpose[{XCORD, YCORD, ZCORD}]

==> {{x1, y1, z1}, {x2, y2, z2}, {x3, y3, z3}}

可以找到有关使用列表的更多信息此处,更具体地针对您的问题,此处

请注意,最好不要以大写字母开头您自己的任何变量(尽管这是允许的)。使用变量名的小写开头意味着您永远不会与数千个均以大写字母开头的内置符号发生冲突。

You can do this with Transpose:

XCORD = {x1, x2, x3};
YCORD = {y1, y2, y3};
ZCORD = {z1, z2, z3};

res = Transpose[{XCORD, YCORD, ZCORD}]

==> {{x1, y1, z1}, {x2, y2, z2}, {x3, y3, z3}}

More information about working with lists can be found here, and more specific to your question, here.

Please note that it's better (though it's allowed) not to start any variable of your own with a uppercase letter. Using a lowercase start of your variable name means you'll never get into conflict with the thousands of built-in symbols that all start with an uppercase letter.

暮年慕年 2024-12-07 01:29:43

使用 MapThread 函数的替代解决方案:

In[2]:= MapThread[List, {{x1, x2, x3}, {y1, y2, y3}, {z1, z2, z3}}]

Out[2]= {{x1, y1, z1}, {x2, y2, z2}, {x3, y3, z3}}

对于大型列表,Transpose 速度要快一个数量级:

In[3]:= With[{n=10^6}, x=RandomReal[1, n]; y=RandomReal[2, n]; z=RandomReal[3, n];]

In[4]:= Transpose[{x, y, z}]; // Timing

Out[4]= {0.644832, Null}

In[5]:= MapThread[List, {x, y, z}]; // Timing

Out[5]= {5.87969, Null}

Alternate solution using the MapThread function:

In[2]:= MapThread[List, {{x1, x2, x3}, {y1, y2, y3}, {z1, z2, z3}}]

Out[2]= {{x1, y1, z1}, {x2, y2, z2}, {x3, y3, z3}}

For large lists, Transpose is an ordner of a magnitude faster, though:

In[3]:= With[{n=10^6}, x=RandomReal[1, n]; y=RandomReal[2, n]; z=RandomReal[3, n];]

In[4]:= Transpose[{x, y, z}]; // Timing

Out[4]= {0.644832, Null}

In[5]:= MapThread[List, {x, y, z}]; // Timing

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