简体   繁体   中英

Rearrange subarrays in a numpy array?

I have a numpy array with shape (6023, 6023). In reality it is a combination of a few "subarrays", meaning I can attribute a name to a few parts of each side of the array, and the subarrays show the interactions of these "names". Names are identical on both sides (and the data is simmetrical around it's main diagonal). This is what the array actually consists of. These are the coordinates of the boundaries of it's parts I call subarrays and their respective names:

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

So in the very corner there is a square of data with interaction of '4' and '4'. It is close to '4'-'X', 'X'-'4' and 'X'-'X'. And so on. If it is all unclear I could make a picture which will probably clarify what I mean.

So the question is: how do I rearrange the subarrays, so that the names go in a different order? I wish them to be in the following order: ('2L', '2R', '3L', '3R', '4', 'X')

UPDATE 1

Before I post a picture, I will try to explain it differently. The situation is essentially equivalent to this: I have 6*6=36 arrays, which correspond to interactions: '2L'-'2L', '2L'-'2R', '2L'-'3L', ..., 'X'-'X'. How do I make an array with all these arrays, so that they are all located logically consistent and in the order of names ('2L', '2R', '3L', '3R', '4', 'X') on each side of the final array?

UPDATE 2

I've made a scheme of what I need, hopefully it will clear it up.

This is what I have in the beginning: 在此处输入图片说明

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.

This will do the rearrangement of the names and the blocks of the input array - the key in the block rearrangement is the numpy.ix_ function which allows for Matlab-like indexing.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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