3
我有一些簡單的代碼,我只想計算兩個矩陣乘積中不同列的數量。該代碼是計算矩陣中不同列的數量
import numpy as np
import itertools
n = 5
h = 2
M = np.random.randint(2, size=(h,n))
F = np.matrix(list(itertools.product([0,1],repeat = 5))).transpose()
product = M*F
setofcols = set()
for column in product.T:
setofcols.add(column)
print len(setofcols)
然而,這給出了錯誤的答案爲setofcols
所有元素都是即使列是相同的不同。什麼是正確的做法?
我會用更大的n和h值來運行它,所以儘可能少使用內存的解決方案會很好。
相關:http://stackoverflow.com/questions/12983067/how-to-find-unique-vectors-of-a-2d-array-over-a-particular-axis-in-a-vectorized –
Do你的意思是每列都包含獨特的內容? – dawg
使用'setofcols.add(repr(column))'Answer是'10' – dawg