2012-03-03 12 views
0

私は2つ(以上)のベクトルaとbのデカルト積cを持っています。私は、[:: i]とb [:: j]のデカルト積をcから得たいと思います。numpyサブディッピングとデカルト積

これは、新しいデカルト積が、i番目のアイテムとすべてのj番目のアイテムをスキップすることを意味します。例えば

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]]) 

ゼロからCアレイ算出することなくget_subarray作成する方法すべてのアイデア(それは実際ののCrossProductで評価関数でありので、高価である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] 

答えて

0

はix_を用いた一般的なソリューションです。

def get_subarray(c, samples, veclens): 
    n = len(veclens) 
    d = c.reshape(veclens+[n]) 
    i = numpy.ix_(*[range(0, l, s) for l,s in zip(veclens,samples)] 
    return d[i].reshape((-1,n) 
関連する問題