鑑於矩陣移調使用循環
matrix = [[2, None],
[2, None]]
我需要計算的這種轉置的矩陣。我做了以下內容:
def transpose(matrix):
# Makes a copy of the matrix
result = matrix
# Computes tranpose
for i in range(2):
for j in range(2):
result[j][i] = matrix[i][j]
return result
但是這給了我的錯誤結果:
[[2, None],
[None, None]]
,而應該是
[[2, 2],
[None, None]]
誰能告訴我我哪裏錯我的代碼?
你可以用它做了一個通用的矩陣'numpy' ,矩陣= numpy.matrix(矩陣)'和'matrix.T'是結果 –