简体   繁体   English

如何基于另一个 ndarray 最有效地切换一个 ndarray 中的元素

[英]How to most efficiently switch around elements in one ndarray based on another ndarray

I'm having trouble figuring out how to efficently create a copy of a 3d numpy array where a small number of the elements are swapped around.我无法弄清楚如何有效地创建 3d numpy 数组的副本,其中交换了少量元素。

I'd like to be able to do something like the following:我希望能够执行以下操作:

#the matrix to rearange 
a=np.array(
 [[[ 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]]])

#a matric of indicies in a. In this case, [0,1,0] -> [0,0,0] -> [0,2,1] and all the rest are the the same
b=np.array(
[[[[0, 1, 0], [0, 0, 1], [0, 0, 2]],
  [[0, 2, 1], [0, 1, 1], [0, 1, 2]],
  [[0, 2, 0], [0, 0, 0], [0, 2, 2]]],

[[[1, 0, 0], [1, 0, 1], [1, 0, 2]],
 [[1, 1, 0], [1, 1, 1], [1, 1, 2]],
 [[1, 2, 0], [1, 2, 1], [1, 2, 2]]],

[[[2, 0, 0], [2, 0, 1], [2, 0, 2]],
 [[2, 1, 0], [2, 1, 1], [2, 1, 2]],
 [[2, 2, 0], [2, 2, 1], [2, 2, 2]]]])

>>>np.something(a,b,whatever)
>>>np.array(
 [[[ 3,  1,  2],
   [ 7,  4,  5],
   [ 6,  0,  8]],

   [[ 9, 10, 11],
    [12, 13, 14],
    [15, 16, 17]],

   [[18, 19, 20],
    [21, 22, 23],
    [24, 25, 26]]])

I'm also open to having b be full of indicies in the flattened version of a instead of coordinate vectors, but I'm still not sure how/if it could work efficiently.我也愿意让 b 在 a 的扁平版本中充满索引而不是坐标向量,但我仍然不确定它如何/是否可以有效地工作。

Alternatively, if there's a way to make this work, the transformation matrix could be encoded with unit translations like this:或者,如果有办法完成这项工作,则可以使用如下单位翻译对转换矩阵进行编码:

#the matrix to rearange 
a=np.array(
  [[[ 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]]])

 #a transformation matric showing the same [0,1,0] -> [0,0,0] -> [0,2,1], but in terms of displacement. 
#In other words, the data in [0,0,0] is moved down 2 rows and right 1 column to [0,2,0], because b[0,0,0]=[0,2,1]
b=np.array(
[[[[0, 2, 1], [0, 0, 0], [0, 0, 0]],
  [[0, -1, 0], [0, 0, 0], [0, 0, 0]],
  [[0, 0, 0], [0, -1, -1], [0, 0, 0]]],

 [[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
  [[0, 0, 0], [0, 0, 0], [0, 0, 0]],
  [[0, 0, 0], [0, 0, 0], [0, 0, 0]]],

 [[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
  [[0, 0, 0], [0, 0, 0], [0, 0, 0]],
  [[0, 0, 0], [0, 0, 0], [0, 0, 0]]]])


>>>np.something(a,b,whatever)
>>>np.array(
  [[[ 3,  1,  2],
    [ 7,  4,  5],
    [ 6,  0,  8]],

   [[ 9, 10, 11],
    [12, 13, 14],
    [15, 16, 17]],

   [[18, 19, 20],
    [21, 22, 23],
    [24, 25, 26]]])

(Using your first version of a and b ) You are looking for (使用ab第一个版本)您正在寻找

a[tuple(np.moveaxis(b,-1,0))]

this splits b into individual arrays, one for each dimension of a and then uses them to index into a by means of "advanced" or "fancy" indexing.这将b拆分为单独的数组, b的每个维度a ,然后通过“高级”或“花式”索引使用它们来索引到a

Please note that the tuple conversion is important here.请注意, tuple转换在这里很重要。 It changes the way numpy interprets the indices by telling it to treat each element of the tuple as indexing into one dimension.它改变了 numpy 解释索引的方式,告诉它​​把元组的每个元素都当作一个维度的索引。 Left as a single nd array instead it would have been read as all indexing into dimension 0 .保留为单个 nd 数组,而是将其读取为所有索引到维度0 Try it out to get a feel for it!尝试一下,感受一下吧!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM