通过2D索引数组更改2D数组

时间:2018-01-16 10:02:43

标签: python arrays numpy concatenation indices

我有输入数组:

np.random.seed(45)
a = np.random.randint(100,size=(5,21))
print (a)
[[75 30  3 32 95 61 85 35 68 15 65 14 53 57 72 87 46  8 53 12 34]
 [24 12 17 68 30 56 14 36 31 86 36 57 61 79 17  6 42 11  8 49 77]
 [75 63 42 54 16 24 95 63 98 22 27 32 16 75 58 60 54 96 70 32 16]
 [59 92 55 88  5 81 93 79 67 55 60 57 83 27 78 18 87 55 20  9  9]
 [73 27 57 50  7 57 78 68 23 75 41 39 70  2 71 70 27 47 54 93 19]]

索引数组:

b = np.array(
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 
 [9, 8, 7, 6, 5, 4, 3, 2, 1, 0], 
 [20, 19, 18, 17, 16, 15, 14, 13, 12, 11]]
)

print (b)
[[ 0  1  2  3  4  5  6  7  8  9]
 [ 9  8  7  6  5  4  3  2  1  0]
 [20 19 18 17 16 15 14 13 12 11]]

预期产出:

c = np.array(
[[75, 30, 3, 32, 95, 61, 85, 35, 68, 15], 
 [24, 12, 17, 68, 30, 56, 14, 36, 31, 86], 
 [75, 63, 42, 54, 16, 24, 95, 63, 98, 22],
 [59, 92, 55, 88, 5, 81, 93, 79, 67, 55],
 [73, 27, 57, 50, 7, 57, 78, 68, 23, 75], 
 [15, 68, 35, 85, 61, 95, 32, 3, 30, 75], 
 [86, 31, 36, 14, 56, 30, 68, 17, 12, 24],
 [22, 98, 63, 95, 24, 16, 54, 42, 63, 75], 
 [55, 67, 79, 93, 81, 5, 88, 55, 92, 59], 
 [75, 23, 68, 78, 57, 7, 50, 57, 27, 73],
 [34, 12, 53, 8, 46, 87, 72, 57, 53, 14],
 [77, 49, 8, 11, 42, 6, 17, 79, 61, 57], 
 [16, 32, 70, 96, 54, 60, 58, 75, 16, 32],
 [9, 9, 20, 55, 87, 18, 78, 27, 83, 57], 
 [19, 93, 54, 47, 27, 70, 71, 2, 70, 39]]
)
print (c)
#indices - [ 0  1  2  3  4  5  6  7  8  9]
#array is not changed 
[[75 30  3 32 95 61 85 35 68 15]
 [24 12 17 68 30 56 14 36 31 86]
 [75 63 42 54 16 24 95 63 98 22]
 [59 92 55 88  5 81 93 79 67 55]
 [73 27 57 50  7 57 78 68 23 75]
 #indices - [ 9  8  7  6  5  4  3  2  1  0]
 #array is changed first column is a[:, 9], second a[:, 8], ...
 [15 68 35 85 61 95 32  3 30 75]
 [86 31 36 14 56 30 68 17 12 24]
 [22 98 63 95 24 16 54 42 63 75]
 [55 67 79 93 81  5 88 55 92 59]
 [75 23 68 78 57  7 50 57 27 73]
 #indices - [20 19 18 17 16 15 14 13 12 11]
 #array is changed first column is a[:, 20], second a[:, 19], ...
 [34 12 53  8 46 87 72 57 53 14]
 [77 49  8 11 42  6 17 79 61 57]
 [16 32 70 96 54 60 58 75 16 32]
 [ 9  9 20 55 87 18 78 27 83 57]
 [19 93 54 47 27 70 71  2 70 39]]

解释

对于索引数组的每一行,需要更改输入数组的列,最后将所有数组连接在一起。

我相信有一些很好的矢量化解决方案。

我尝试了什么:

我检查了this,但没有帮助。

1 个答案:

答案 0 :(得分:7)

索引到列,置换轴和重塑 -

dialogHeight