gpt4 book ai didi

python - 围绕矩阵周长移动值 - Python/NumPy

转载 作者:行者123 更新时间:2023-12-01 02:38:26 24 4
gpt4 key购买 nike

横向值必须沿周长移动。

例如一步扭转:

1 2 3     4 1 2
4 5 6 ==> 7 5 3
7 8 9 8 9 6

最佳答案

这是2D数组的一种方法 -

def move_perimeter(a, n, direction='clockwise'):
# a : input array
# n : number of elements to be moved
# direction : 'clockwise' or 'counterclockwise'

# Dictionary to map for number of elements rolling later
dirn_map = {'clockwise':n,'counterclockwise':-n}

# Store size
m,n = a.shape
r0 = np.arange(m-1)
r1 = np.arange(n-1)

# Get Top, Right, Bottom and Left side linear indices. Store those.
T = r1
R = n-1+r0*n
B = a.size-r1-1
L = n+r0[::-1]*n
idx = np.r_[T,R,B,L]

# Make a copy of input array
out = a.copy()

# Linearly index into output array with those original indices
# and extract rolled values from input array.
out.ravel()[idx] = a.ravel()[np.roll(idx, dirn_map[direction])]
return out

示例运行 -

In [233]: a
Out[233]:
array([[83, 13, 27, 13],
[90, 78, 57, 68],
[66, 47, 44, 53],
[13, 98, 95, 46],
[29, 87, 80, 92],
[91, 19, 86, 26],
[31, 27, 75, 72]])

In [234]: move_perimeter(a, n=1, direction='clockwise')
Out[234]:
array([[90, 83, 13, 27],
[66, 78, 57, 13],
[13, 47, 44, 68],
[29, 98, 95, 53],
[91, 87, 80, 46],
[31, 19, 86, 92],
[27, 75, 72, 26]])

In [235]: move_perimeter(a, n=2, direction='counterclockwise')
Out[235]:
array([[27, 13, 68, 53],
[13, 78, 57, 46],
[83, 47, 44, 92],
[90, 98, 95, 26],
[66, 87, 80, 72],
[13, 19, 86, 75],
[29, 91, 31, 27]])

关于python - 围绕矩阵周长移动值 - Python/NumPy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45969149/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com