交换包含矩阵的最小和最大元素的行 Python

发布于 2025-01-19 20:12:16 字数 260 浏览 0 评论 0原文

我需要创建矩阵

matrix = np.random.randint(1, 100, size = (3, 3), dtype='l')

,看起来像是

10 45 74
59 20 15
86 41 76

,我需要交换包含最大和最小数字的行 喜欢

86 41 76
59 20 15
10 45 74

我该怎么做?

I need to create matrix

matrix = np.random.randint(1, 100, size = (3, 3), dtype='l')

and it`s looks likee

10 45 74
59 20 15
86 41 76

and i need to swap rows that contains max and min number
like that

86 41 76
59 20 15
10 45 74

how i can do it?

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

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

发布评论

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

评论(2

情仇皆在手 2025-01-26 20:12:16

这是一种(无聊的)解决方案:

import numpy as np

mat = np.array([
  [10, 45, 74],
  [59, 20, 15],
  [86, 41, 76],
])

max_row_id = mat.max(1).argmax()  # row index with maximum element
min_row_id = mat.min(1).argmin()  # row index with minimum element
row_idx = np.arange(mat.shape[0]) # index of row ids
row_idx[max_row_id] = min_row_id  # swap row ids for rows with min and max elements
row_idx[min_row_id] = max_row_id

result = mat[row_idx,:]

Here is one (boring) solution:

import numpy as np

mat = np.array([
  [10, 45, 74],
  [59, 20, 15],
  [86, 41, 76],
])

max_row_id = mat.max(1).argmax()  # row index with maximum element
min_row_id = mat.min(1).argmin()  # row index with minimum element
row_idx = np.arange(mat.shape[0]) # index of row ids
row_idx[max_row_id] = min_row_id  # swap row ids for rows with min and max elements
row_idx[min_row_id] = max_row_id

result = mat[row_idx,:]
情绪失控 2025-01-26 20:12:16

我认为使用 np.unravel_index 是除了以下高级索引来交换行之外最快的方法之一a>:

row_min = np.unravel_index(np.argmin(mat), mat.shape)[0]
row_max = np.unravel_index(np.argmax(mat), mat.shape)[0]
mat[[row_min, row_max]] = mat[[row_max, row_min]]

基准(Colab):

# matrix (3*3)                           FASTEST
1000 loops, best of 5: 8.7 µs per loop   ------> hilberts_drinking_problem method
1000 loops, best of 5: 14.3 µs per loop

#  matrix (1000*3)  
100 loops, best of 5: 65   µs per loop
100 loops, best of 5: 21.9 µs per loop   ------> This method 

# matrix (1000*1000)
100 loops, best of 5: 3.44 ms per loop
100 loops, best of 5: 2.64 ms per loop   ------> This method

# matrix (10000*10000)
10 loops, best of 5: 388 ms per loop
10 loops, best of 5: 282 ms per loop     ------> This method

I think using np.unravel_index be one of the fastest way besides the following advanced indexing to swap the rows:

row_min = np.unravel_index(np.argmin(mat), mat.shape)[0]
row_max = np.unravel_index(np.argmax(mat), mat.shape)[0]
mat[[row_min, row_max]] = mat[[row_max, row_min]]

Benchmarks (Colab):

# matrix (3*3)                           FASTEST
1000 loops, best of 5: 8.7 µs per loop   ------> hilberts_drinking_problem method
1000 loops, best of 5: 14.3 µs per loop

#  matrix (1000*3)  
100 loops, best of 5: 65   µs per loop
100 loops, best of 5: 21.9 µs per loop   ------> This method 

# matrix (1000*1000)
100 loops, best of 5: 3.44 ms per loop
100 loops, best of 5: 2.64 ms per loop   ------> This method

# matrix (10000*10000)
10 loops, best of 5: 388 ms per loop
10 loops, best of 5: 282 ms per loop     ------> This method
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文