找到最大的元素,并在Python中与第一个交换
我想找到r2
的最大元素,并将其与r2 [0,0]
的元素交换。我提出了预期的输出。
import numpy as np
r2 = np.array([[ 1.00657843, 63.38075613, 312.87746691],
[375.25164461, 500. , 125.75493382],
[437.6258223 , 250.50328922, 188.12911152]])
indices = np.where(r2 == r2.max())
预期的输出是
array([[ 500., 63.38075613, 312.87746691],
[375.25164461, 1.00657843, 125.75493382],
[437.6258223 , 250.50328922, 188.12911152]]
I want to locate the largest element of r2
and swap it with the element at r2[0,0]
. I present the expected output.
import numpy as np
r2 = np.array([[ 1.00657843, 63.38075613, 312.87746691],
[375.25164461, 500. , 125.75493382],
[437.6258223 , 250.50328922, 188.12911152]])
indices = np.where(r2 == r2.max())
The expected output is
array([[ 500., 63.38075613, 312.87746691],
[375.25164461, 1.00657843, 125.75493382],
[437.6258223 , 250.50328922, 188.12911152]]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您只需返回最大值的索引,然后与第一个索引交换
You could just return the index of the largest value and then swap with first index
您可以将最大值存储在变量中,然后将
[0,0]
元素分配给您使用where
的索引,然后设置[ 0,0]
元素到存储的最大值:输出:
You can store the maximum value in a variable, and then assign the
[0, 0]
element to the indices which you found withwhere
and then set the[0, 0]
element to the stored maximum value:Output:
这样您将替换所有具有最大值的元素
In this way you will replace all the element that have the max value