Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将二维numpy数组中的元素旋转';n';单位?_Python_Arrays_Numpy - Fatal编程技术网

Python 如何将二维numpy数组中的元素旋转';n';单位?

Python 如何将二维numpy数组中的元素旋转';n';单位?,python,arrays,numpy,Python,Arrays,Numpy,1个单位的旋转应给出: x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] x = [[a, b, c, d], [e, f, g, h], [i, j, k, l], [m, n, o, p]] 基本上,我想将数组中的每个圆形层移动“n”个单位 我查看了numpy.roll,但不知道如何在这个用例中使用它。 我不能使用像scipy.ndimage.interpolation.rotate这样的图像旋转例程,因为它们改

1个单位的旋转应给出:

x = [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]
x = [[a, b, c, d],
     [e, f, g, h],
     [i, j, k, l],
     [m, n, o, p]]
基本上,我想将数组中的每个圆形层移动“n”个单位

我查看了numpy.roll,但不知道如何在这个用例中使用它。 我不能使用像scipy.ndimage.interpolation.rotate这样的图像旋转例程,因为它们改变了形状,并没有完全达到预期的结果

编辑:

对于4 X 4矩阵:

x = [[4, 1, 2],
     [7, 5, 3],
     [8, 9, 6]]
1个单位的旋转应给出:

x = [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]
x = [[a, b, c, d],
     [e, f, g, h],
     [i, j, k, l],
     [m, n, o, p]]
编辑:

添加了一些关于这如何适用于任意大小的说明


对于旋转1个单位的nxn矩阵,外“环”首先移动1。剩余的“内部”(N-2)X(N-2)矩阵递归地遵循相同的逻辑。

对于3x3阵列,您可以使用和来完成此操作:

旋转1个单位:

>>> x = np.arange(1, 10).reshape((3, 3))
>>> x
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
>>> i = np.array([0, 1, 2, 5, 8, 7, 6, 3])  # Indices in circular order
>>> x.flat[i]
array([1, 2, 3, 6, 9, 8, 7, 4])
旋转4个单位:

>>> x.flat[i] = np.roll(x.flat[i], 1)  # Rotate indices and reassign
>>> x
array([[4, 1, 2],
       [7, 5, 3],
       [8, 9, 6]])

对于4x4的情况,我仍然需要弄清楚每个“圆”是否需要根据其长度以不同的“速度”旋转…

这里有一种方法,假设您希望旋转,以便各片之间的偏移量保持不变,其中,切片是指从中心向外的元素的最外层-

>>> x.flat[i] = np.roll(x.flat[i], 4)
>>> x
array([[9, 8, 7],
       [6, 5, 4],
       [3, 2, 1]])
样本运行

案例1(3 x 3阵列):

案例2(4 x 4阵列):


你在矩阵上应用排列。置换通常由向量(i-->p[i])表示,并应用于向量。如果需要,可以在矩阵中表示矩阵的置换,置换将是成对矩阵,因此(i,j)处的元素移动到m[i,j]


构建矩阵后,只需简单的循环即可应用排列。

我不熟悉numpy,但数组旋转是否会影响其中的元素?因为原始数组中有9,但旋转后的版本中没有。我真的不确定,只是问一下,以防这可能是一个错误。你能展示一个4x4阵列的示例吗?你能检查你的结果吗?它应该是
[[4,1,2],[7,5,3],[8,9,6]]
?因为现在,我无法理解…你是对的。不知道我怎么搞砸了。修正。编辑添加了4 X 4案例的示例。这是一个有趣的方法。我相信这也是@Praveen的想法。
In [444]: x
Out[444]: 
array([[24, 85, 97],
       [51, 33, 11],
       [86, 38, 33]])

In [445]: rotate_steps(x,shift=1)
Out[445]: 
array([[51, 24, 85],
       [86, 33, 97],
       [38, 33, 11]])
In [447]: x
Out[447]: 
array([[11, 70, 28, 13],
       [44, 41, 17, 82],
       [47, 32, 89, 25],
       [32, 20, 67, 98]])

In [448]: rotate_steps(x,shift=1)
Out[448]: 
array([[44, 11, 70, 28],
       [47, 32, 41, 13],
       [32, 89, 17, 82],
       [20, 67, 98, 25]])

In [449]: rotate_steps(x,shift=2)
Out[449]: 
array([[47, 44, 11, 70],
       [32, 89, 32, 28],
       [20, 17, 41, 13],
       [67, 98, 25, 82]])