擅长:python、mysql、java
<p>试试这个:</p>
<pre><code>import numpy as np
def swap_rdi(mat, index):
row, col = index
rows, cols = mat.shape
assert(row + 1 != rows and col + 1 != cols)
mat[row, col], mat[row+1, col+1] = mat[row+1, col+1], mat[row, col]
return
</code></pre>
<p>例如:</p>
<pre><code>mat = np.matrix([[1,2,3], [4,5,6]])
print('Before:\n{}'.format(mat))
print('After:\n{}'.format(swap_rdi(mat, (0,1))))
</code></pre>
<p>产出:</p>
<pre><code>Before:
[[1 2 3]
[4 5 6]]
After:
[[1 6 3]
[4 5 2]]
</code></pre>