あなたウォン:
私はマスクとインデックスを持つ配列に配列を分割したい
以下のような
a = array([ 0, 1, 2, 3, 4, 5]))
b = [0,2,3]
に
c = array([[0, 2, 3], [1, 3, 4], [2, 4, 5]])
私はループせずにこれを行うことができますか?
編集:
より多くの例...
ちょっと、配列持ちa
形状の[10, 10, 10]
どこにa[x, y, :] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
今、マスクを与えられました b = [0, 3, 7]
私は、出力は配列になりたいc
な形状の[10, 10, 3, 3]
場所c[x, y, :, :] = [[0, 3, 7], [1, 4, 8], [2, 5, 9]]
LudvigH:
あなたは、生成することができますb
あなたが望むインデックス間放送さの和、およびシフトベクトルとして。そして、あなたはより大きなサイズに再放送することができます。あなたの例の出力は依存していないのでa
、配列、私はそれを無視しています。
from numpy import array, broadcast_to, arange
from numpy.random import random
a = random((10,10,10)) # not used on the code at all.... don't understand what it is for...
b = [0,2,3]
b_array = array(b)
b_shifts = arange(3).reshape(-1,1)
c_cell= b+b_shifts # here, they are broadcasted toegether. one is a row-vector and one is a column-vector...
c = broadcast_to(c_cell,(10,10,3,3))
あなたが作成することができますb_shifts
いくつかの他の方法はようにステップサイズに依存し、と...
EDITはあなたのコメントに基づいて、より正確な答えがあるようです。
from numpy import array, arange
a = arange(2*2*10).reshape((2,2,10)) # some example input
b = array([0,2,3]) # the 'template' to extract
shifts = arange(3).reshape(-1,1) # 3 is the number of repeats
indexer = b+shifts # broadcasted sum makes a matrix
c = a[:,:,indexer] # extract
これは、かかるb
テンプレートの種類として配列を、そして特定のシフトでそれを繰り返します。最後に、それはすべての配列から、それらのエントリを抽出しますa[i,j,:]
にc[i,j,:,:]
。上記からOtputは以下のとおりです。
print(a)
[[[ 0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]]
[[20 21 22 23 24 25 26 27 28 29]
[30 31 32 33 34 35 36 37 38 39]]]
print(c)
[[[[ 0 2 3]
[ 1 3 4]
[ 2 4 5]]
[[10 12 13]
[11 13 14]
[12 14 15]]]
[[[20 22 23]
[21 23 24]
[22 24 25]]
[[30 32 33]
[31 33 34]
[32 34 35]]]]