在 Mathematica 8 中搜索矩阵 - 尝试查找与 X 同一行的其他元素

发布于 2024-12-26 05:29:34 字数 752 浏览 2 评论 0原文

如果有人感兴趣的话,斜体文字描述了我的总体目标。问题在下面。

我正在尝试使用 Mathematica 8 绘制简单分子的能级图。我的方法很粗糙,如下所示:

  1. 查找简单 Hückel 矩阵的特征值。
  2. 删除重复项并确定列表的大小。
  3. 通过比较重复列表与无重复列表来评估简并数。
  4. 创建一个 x 2 零矩阵,其中 n 是独特的能量水平。

5。用独特的能级填充第一列,用简并性填充第二列。

步骤 5 中生成的矩阵可以如下所示:

(1  2)
(3  1)   ==    M
(-1 1)

我希望计算第 2 列的最大值,然后找到同一行中元素的值,但在第 1 列中。在本例中,我要查找的答案是 1。

这些命令的计算结果均为 -1:

Extract[M[[All, 1]], M[[Max[M[[All, 2]]], 1]]]
M[[Max[M[[All, 1]]], 1]]

这不是我想要的答案。

有什么建议吗?

编辑:这

Part[Part[Position[M, Max[M[[All, 2]]]], 1], 1]

可行,但我不明白为什么我必须使用 Part[] 两次。

The text in italics describes my general goal, if anyone is interested. Question is underneath.

I am trying to graph the energy levels of simple molecules using Mathematica 8. My method is crude, and goes as this:

  1. Find eigenvalues of simple Hückel matrix.
  2. Delete duplicates and determine size of list.
  3. Evaluate the number of degeneracies by comparing duplicate list with no-duplicate list.
  4. Create a n x 2 zero matrix where n is the number of unique energy levels.

5. Fill first column with unique energy levels, second column with degeneracies.

The matrix generated in step 5 can look like this:

(1  2)
(3  1)   ==    M
(-1 1)

I wish to evaluate the maximum of column 2, and then find the value of the element in the same row, but in column 1. In this case, the answer I am looking for is 1.

These commands both evaluate to -1:

Extract[M[[All, 1]], M[[Max[M[[All, 2]]], 1]]]
M[[Max[M[[All, 1]]], 1]]

which is not the answer I want.

Any tips?

EDIT: This

Part[Part[Position[M, Max[M[[All, 2]]]], 1], 1]

works, but I don't understand why I have to use Part[] twice.

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

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

发布评论

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

评论(3

下壹個目標 2025-01-02 05:29:34
m = {{1, 2}, {3, 1}, {-1, 1}}
max = Max[m[[All, 2]]]

因此找到最大值的位置并将第二列替换为第一列:

pos=Position[m, max] /. {x_,_}:>{x,1}
{{1,1}}

然后从 pos 中取出第一个元素,即 {1,1} 并在 Part 中使用它

m[[Sequence @@ First[pos]]]
1

但是话虽如此,我更喜欢一些东西像这样:

Cases[m, {x_, max} :> x]
{1}

结果是一个列表。您可以使用 First@Cases[...] 或者您可能希望保留结果列表以涵盖最大值在列中出现多次的情况。

m = {{1, 2}, {3, 1}, {-1, 1}}
max = Max[m[[All, 2]]]

So find the position of the max and replace the second column with the first:

pos=Position[m, max] /. {x_,_}:>{x,1}
{{1,1}}

Then take the first element from pos, i.e. {1,1} and sub use it in Part

m[[Sequence @@ First[pos]]]
1

But having said that I prefer something like this:

Cases[m, {x_, max} :> x]
{1}

The result is a list. You could either use First@Cases[...] or you might want to keep a list of results to cover cases where the maximum value occurs more than once in a column.

怪异←思 2025-01-02 05:29:34

内部 Part 为您提供第一次出现的最大值。 Position 返回位置列表,即使只有一个元素具有最大值,如下所示:

 M = {{2, 2}, {2, 3}, {2, 2}, {1, 1}}

 {{2, 2}, {2, 3}, {2, 2}, {1, 1}}

Position[M, Max[M[[All, 2]]]]

 {{2, 2}}

因此,您需要此输出的第一个元素中的第一个元素。您可以像这样压缩代码:

Position[M, Max[M[[All, 2]]]][[1, 1]]

但是,我认为您的代码需要更好地处理的一件事是这种情况:

 M = {{3, 2}, {2, 3}, {2, 2}, {1, 1}}

3, 2}, {2, 3}, {2, 2}, {1, 1}}

Position[M, Max[M[[All, 2]]]]

{{1, 1}, {2, 2}}

在这种情况下,您的代码将得到错误的答案。

更好的是:

M[[All, 1]][[Position[M[[All, 2]], Max[M[[All, 2]]]][[1, 1]] ]]

或者

M[[Position[M[[All, 2]], Max[M[[All, 2]]]][[1, 1]], 1]]

The inner Part gives you the first occurance of the maximum. Position returns a list of positions, even if there is only one element that has the maximum value, like this:

 M = {{2, 2}, {2, 3}, {2, 2}, {1, 1}}

 {{2, 2}, {2, 3}, {2, 2}, {1, 1}}

Position[M, Max[M[[All, 2]]]]

 {{2, 2}}

So you want the first element in the first element of this output. You could condense your code like this:

Position[M, Max[M[[All, 2]]]][[1, 1]]

However, one thing that I think your code needs to handle better is this case:

 M = {{3, 2}, {2, 3}, {2, 2}, {1, 1}}

3, 2}, {2, 3}, {2, 2}, {1, 1}}

Position[M, Max[M[[All, 2]]]]

{{1, 1}, {2, 2}}

You will get the wrong answer with your code in this case.

Better would be:

M[[All, 1]][[Position[M[[All, 2]], Max[M[[All, 2]]]][[1, 1]] ]]

Or alternatively

M[[Position[M[[All, 2]], Max[M[[All, 2]]]][[1, 1]], 1]]
飘然心甜 2025-01-02 05:29:34

如果您只想在第二列中出现重复最大值的情况下使用单个列一个值,我建议您使用 Ordering

m = {{1, 3}, {1, 8}, {5, 7}, {2, 2}, {1, 9}, {4, 9}, {5, 6}};

m[[ Ordering[m[[All, 2]], -1], 1 ]]
{4}

If you only want a single column one value in the case of duplicate maximum values in column two I suggest that you make use of Ordering:

m = {{1, 3}, {1, 8}, {5, 7}, {2, 2}, {1, 9}, {4, 9}, {5, 6}};

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