我有問題要問這個矩陣:我怎樣才能獲得真正的矩陣
A=([[2, 3, 4, 2, 1, 3, 4, 1, 3, 2 ]])
我想從A如下得到另一個矩陣:
B=([[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1],
[0, 1, 0, 0],
[1, 0, 0, 0],
[0, 0, 1, 0],
[0, 1, 0, 1],
[1, 0, 0, 0],
[0, 0, 1, 0],
[0, 1, 0, 0]])
Iwritten此:
import numpy as np
n=np.matrix('[2, 3, 4, 2, 1, 3, 4, 1, 3, 2]')
c=np.matrix('[0, 0, 0, 0]')
d=np.zeros((1,4))
for i in np.nditer(n):
h=d.itemset((0,i-1),1)
print d
但我得到錯誤的matris如下
[[ 0. 1. 0. 0.]]
[[ 0. 1. 1. 0.]]
[[ 0. 1. 1. 1.]]
[[ 0. 1. 1. 1.]]
[[ 1. 1. 1. 1.]]
[[ 1. 1. 1. 1.]]
[[ 1. 1. 1. 1.]]
[[ 1. 1. 1. 1.]]
[[ 1. 1. 1. 1.]]
[[ 1. 1. 1. 1.]]
我怎樣才能獲得真正的(B)矩陣?
行'[0,1,0,1]'對嗎? – DSM
簡短回答:在'for'循環中放入'd = np.zeros((1,4))',所以1不會累加。但是,你仍然沒有創建一個矩陣,只是打印出它的行。看到各種答案。 – greggo