在numpy数组中重新排列子数组?

菲利亚

我有一个形状为(6023,6023)的numpy数组。实际上,它是几个“子数组”的组合,这意味着我可以将名称归因于数组每一侧的几个部分,并且子数组显示了这些“名称”的相互作用。双方的名称都是相同的(并且数据在主对角线周围是对称的)。这就是数组的实际组成。这些是我称之为子数组的部分边界的坐标及其各自的名称:

boundaris = [(0, 68), (68, 1190), (1190, 2248), (2248, 3399), (3399, 4795), (4795, 6023)]
names = ('4', 'X', '2R', '2L', '3R', '3L')

因此,在拐角处有一个交互作用为“ 4”和“ 4”的数据正方形。它接近“ 4”-“ X”,“ X”-“ 4”和“ X”-“ X”。等等。如果还不清楚,我可以拍张照片,这可能会澄清我的意思。

所以问题是:如何重新排列子数组,以使名称以不同的顺序排列?我希望它们按以下顺序排列:(“ 2L”,“ 2R”,“ 3L”,“ 3R”,“ 4”,“ X”)

更新1

在发布图片之前,我将尝试以不同的方式解释它。这种情况基本上等效于此:我有6 * 6 = 36个数组,它们对应于交互:'2L'-'2L','2L'-'2R','2L'-'3L',..., 'X'-'X'。如何使用所有这些数组制作一个数组,以使它们在逻辑上均位于逻辑一致的位置,并按名称的顺序排列(“ 2L”,“ 2R”,“ 3L”,“ 3R”,“ 4”,“ X”)在最终阵列的每一侧?

更新2

我已经针对我的需要制定了一个计划,希望它可以解决这个问题。

这是我一开始所拥有的: 在此处输入图片说明

Small rectangles inside the big square represent the 'subarrays'. Each of them contains many 'cells', for example, '4'-'4' (the smallest one) contains 68^2=4624 cells. The whole array contains 6023^2=36276529 'cells'.

Then I want to rearrange the subarrays, so that the names go in a different order: 在此处输入图片说明

This is how it should look after the transformation. I happen to know the final coordinates for the boundaries of names, but they are not difficult to calculate. Hopefully, you can see what I want to do: rearrange parts of a big array (essentially, smaller arrays), so that they form an array with different location of 'names' along it's axes.

chthonicdaemon

这将对输入数组的名称和块进行重新排列-块重新排列中的键是numpy.ix_允许类似Matlab的索引的功能。

boundaris = [(0, 68), (68, 1190), (1190, 2248), (2248, 3399), (3399, 4795), (4795, 6023)]
names = ('4', 'X', '2R', '2L', '3R', '3L')
A = numpy.random.random((6023, 6023))

neworder = [3, 2, 5, 4, 0, 1]

def rearrange(l):
    return [l[i] for i in neworder]

newnames = rearrange(names)
ranges = numpy.concatenate([numpy.arange(l, u) for l, u in rearrange(boundaris)])
newA = A[numpy.ix_(ranges, ranges)]

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章