在Matlab中/八度音功能不是,spdiags([-8037.500 50.000 -12.500], 0:2, 1, 51)
給出以下輸出:spdiags()如預期工作在Python
(1, 1) -> -8037.5
(1, 2) -> 50
(1, 3) -> -12.500
然而,當我使用在Python下面,它不會產生類似的結果作爲在Matlab /八度:
Python的spdiags()產生下面的輸出,這是在缺少一號50
和-12.5
條款和第二指數:
array([[-8037.5, 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. ]])
我看了一下this對一個類似問題的回答,但我不確定我哪裏出錯了。
編輯:
我想建立如下圖所示的是由A_diag1
,A_diag2
和A_diag3
矩陣A
。按照答案中的建議,我定義了A_diag1
和A_diag3
。
import numpy as np
import scipy as sp
A_diag1 = np.tile(np.array([-8037.500, 50, -12.5]), (3,1))
A_diag2 = np.reshape(np.repeat([1250, -18505, 1250], 49), (3, 49))
A_diag3 = np.tile(np.array([12.5, -50, 8037.500]), (3,1))
A = np.concatenate((sp.sparse.spdiags(A_diag1, np.r_[0:2 + 1], 1, 51).toarray(), \
sp.sparse.spdiags(A_diag2, np.r_[0:2 + 1], 49, 51).toarray(), \
sp.sparse.spdiags(A_diag3, np.r_[48:50 + 1], 1, 51).toarray()), axis=0)
然而,五顯現細胞在過去的3行和A
列顯示爲零/單數顯示在下面的快照。我預計當前顯示爲零的突出顯示的單元格不爲零。 [您可以複製並粘貼上面的代碼片段以再現A
矩陣,從中可以獲取如下所示的快照。]
EDIT2:
繼使用sp.sparse.diags()
按預期工作代碼。與sp.sparse.spdiags
不同,使用sp.sparse.diags()
時,輸入參數的結果形狀(數組維數)必須在列表中。
import numpy as np
import scipy as sp
A_diag1 = np.array([[-8037.500], [50], [-12.5]])
A_diag2 = np.reshape(np.repeat([1250, -18505, 1250], 49), (3, 49))
A_diag3 = np.array([[12.5], [-50], [8037.500]])
A = np.concatenate((sp.sparse.diags(A_diag1, np.arange(0, 2 + 1), [1, 51]).toarray(), \
sp.sparse.diags(A_diag2, np.arange(0, 2 + 1), [49, 51]).toarray(), \
sp.sparse.diags(A_diag3, np.arange(48, 50 + 1), [1, 51]).toarray()), axis=0)
謝謝。你能否看看爲什麼在編輯下描述的矩陣'A'沒有按預期給出正確的輸出? – user11
'sparse.diags'可能是您想要使用的函數,而不是'spdiags'。 – hpaulj
是的,'sp.sparse.diags'按照建議運行良好。謝謝。 – user11