在mathematica中使用嵌套数组和列表

发布于 2024-12-04 17:10:16 字数 392 浏览 1 评论 0原文

这里绝对是初学者的问题。 我在数学中有两个列表。 第一个是由 Table 命令生成的:

Table[QP[[i]], {i, 10}] 它生成列表:

{52.5, 45., 37.5, 30., 22.5, 15., 7.5, 0., -7.5, -15.}

第二个是 Range

Range[ 0, 9, 1]

生成 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

我需要将这些放入列表列表中。即 {{0,52.5},{1,45} ... } 等。但我似乎无法理解。需要使用循环吗?因为我认为我想要的东西可以用 Table 和 Array 命令生成。

谢谢

Absolute beginner question here.
I have two lists in mathematica.
The first one was generated by the Table command:

Table[QP[[i]], {i, 10}]
which generates the list:

{52.5, 45., 37.5, 30., 22.5, 15., 7.5, 0., -7.5, -15.}

the second is a Range

Range[0, 9, 1]

which generates {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

I need to get these into a list of lists. i.e. {{0,52.5},{1,45} ... } etc. But I can't seem to get it. Do you need to use loops? Because I think that what I want can be generated with the Table and Array commands.

Thanks

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

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

发布评论

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

评论(4

無處可尋 2024-12-11 17:10:16

Transpose 可能是你想要的:

list1 = {52.5, 45., 37.5, 30., 22.5, 15., 7.5, 0., -7.5, -15.}

list2 = Range[0, 9, 1]
Transpose[{list2, list1}]

给出

{{0, 52.5}、{1, 45.}、{2, 37.5}、{3, 30.}、{4, 22.5}、{5, 15.}、{6,
7.5}、{7、0.}、{8、-7.5}、{9、-15.}}

Transpose may be what you want:

list1 = {52.5, 45., 37.5, 30., 22.5, 15., 7.5, 0., -7.5, -15.}

list2 = Range[0, 9, 1]
Transpose[{list2, list1}]

gives

{{0, 52.5}, {1, 45.}, {2, 37.5}, {3, 30.}, {4, 22.5}, {5, 15.}, {6,
7.5}, {7, 0.}, {8, -7.5}, {9, -15.}}

灵芸 2024-12-11 17:10:16

Table 的第一个参数可以是任何表达式。您可以通过指定列表作为第一个参数来让它输出列表的列表:

Table[{i-1, QP[[i]]}, {i, 10}]
(* {{0, QP[[1]]}, {1, QP[[2]]}, ... {8, QP[[9]]}, {9, QP[[10]]}} *)

The first parameter of Table can be any expression. You can have it output a list of lists, by specifying a list as the first parameter:

Table[{i-1, QP[[i]]}, {i, 10}]
(* {{0, QP[[1]]}, {1, QP[[2]]}, ... {8, QP[[9]]}, {9, QP[[10]]}} *)
焚却相思 2024-12-11 17:10:16
Thread[List[Range[0, 9], QP[[;; 10]]]]
Thread[List[Range[0, 9], QP[[;; 10]]]]
故人如初 2024-12-11 17:10:16

要完成方法的阐述,您可以使用 MapIndexed

MapIndexed[{First[#2] - 1,#1}&, data]

其中

data = {52.5, 45., 37.5, 30., 22.5, 15., 7.5, 0., -7.5, -15.}

或者,您可以使用 MapThread

MapThread[List, {Range[0,9], data}]

虽然, MapIndexed 更合适,因为它不需要您生成额外的列表。

我想说的最后一点是,您的代码 Table[QP[[i]], {i, 10}] 意味着 QP 本身是一个列表。 (双括号 [[ ]] 泄露了它。)如果这是正确的,那么 Table 不是生成子集的最佳方法,您可以使用部分 ([[ ]]< /a>) 和 Span 直接

QP[[ 1 ;; 10 ]]

或者

QP[[ ;; 10 ]]

然后替换data即可 在具有这些形式的代码的第一位中。

To complete the exposition of methods, you could use MapIndexed

MapIndexed[{First[#2] - 1,#1}&, data]

where

data = {52.5, 45., 37.5, 30., 22.5, 15., 7.5, 0., -7.5, -15.}

Or, you could use MapThread

MapThread[List, {Range[0,9], data}]

Although, MapIndexed is more appropriate since it does not require you to generate an extra list.

A last point I want to make is that your code Table[QP[[i]], {i, 10}] implies that QP itself is a list. (The double brackets, [[ ]], gave it away.) If that is correct, than Table isn't the best way to generate a subset, you can use Part ([[ ]]) along with Span directly

QP[[ 1 ;; 10 ]]

or

QP[[ ;; 10 ]]

Then, you can replace data in the first bits of code with either of those forms.

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