0
所以我有兩個(或更多)向量a和b的笛卡爾積c。我想從c獲得[:: i]和b [:: j]的笛卡爾積。numpy子網格笛卡爾積
這意味着新的笛卡爾產品將跳過每個第i個項目和每個第j個b項目。
例如
veclens = (3,6)
# <code that generates cross product c here> (I have that).
# result:
c = array([
[0,0],
[0,1],
[0,2],
[0,3],
[0,4],
[0,5],
[1,0],
[1,1],
[1,2],
[1,3],
[1,4],
[1,5],
[2,0],
[2,1],
[2,2],
[2,3],
[2,4],
[2,5]])
print c.shape
(18, 2)
samples = (2,2) # so we want every 2nd item a, and every 2nd in b
# this is the function I would like:
d = get_subarray(c, samples, veclens)
# and now d is something like
array([
[0,0],
[0,2],
[0,4],
[2,0],
[2,2],
[2,4]])
不知道如何寫get_subarray而不計算從頭數組c(這是昂貴的,因爲它實際上是在a的交叉積評價函數和b)。當然,有一些索引技巧?
我正在尋找類似以下的東西,但更一般,更優雅,更快。
def get_subarray(c, samples, veclens):
indexes = []
for i in range(0, veclens[0], samples[0]):
for j in range(0, veclens[1], samples[1]):
indexes.append(i * veclens[1] + j)
return c[indexes]