找到最大的元素,并在Python中与第一个交换

发布于 2025-02-13 01:11:36 字数 503 浏览 1 评论 0原文

我想找到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 技术交流群。

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

发布评论

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

评论(3

靑春怀旧 2025-02-20 01:11:37

您只需返回最大值的索引,然后与第一个索引交换

# get index of largest value. 
index = np.unravel_index(r2.argmax(), r2.shape)
# swap with item at index 0,0
r2[index], r2[0,0] = r2[0,0], r2[index]

You could just return the index of the largest value and then swap with first index

# get index of largest value. 
index = np.unravel_index(r2.argmax(), r2.shape)
# swap with item at index 0,0
r2[index], r2[0,0] = r2[0,0], r2[index]
美煞众生 2025-02-20 01:11:37

您可以将最大值存储在变量中,然后将[0,0]元素分配给您使用where的索引,然后设置[ 0,0]元素到存储的最大值:

maximum = r2.max()
indices = np.where(r2 == maximum)
r2[indices] = r2[0, 0]
r2[0, 0] = maximum
r2

输出:

array([[500.        ,  63.38075613, 312.87746691],
       [375.25164461,   1.00657843, 125.75493382],
       [437.6258223 , 250.50328922, 188.12911152]])

You can store the maximum value in a variable, and then assign the [0, 0] element to the indices which you found with where and then set the [0, 0] element to the stored maximum value:

maximum = r2.max()
indices = np.where(r2 == maximum)
r2[indices] = r2[0, 0]
r2[0, 0] = maximum
r2

Output:

array([[500.        ,  63.38075613, 312.87746691],
       [375.25164461,   1.00657843, 125.75493382],
       [437.6258223 , 250.50328922, 188.12911152]])
养猫人 2025-02-20 01:11:37

这样您将替换所有具有最大值的元素

#get the max value of the array
max_value = np.max(r2)
#all the element with the max value will be replaced with the first value
r2[r2 == max_value] = r2[0][0]
#put the max value in the first position
r2[0][0] = max_value

In this way you will replace all the element that have the max value

#get the max value of the array
max_value = np.max(r2)
#all the element with the max value will be replaced with the first value
r2[r2 == max_value] = r2[0][0]
#put the max value in the first position
r2[0][0] = max_value
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文