Pavel Antspovich
Pavel Antspovich

Reputation: 1221

Numpy: reshape matrix by blocks

I have a matrix

[
    [ [1, 2, 3],    [4, 5, 6],    [7, 8, 9],    [10, 11, 12] ],
    [ [13, 14, 15], [16, 17, 18], [19, 20, 21], [22, 23, 24] ]
]

and I want to get

[
    [ [1, 2, 3], [4, 5, 6], [13, 14, 15], [16, 17, 18] ],
    [ [19, 20, 21], [22, 23, 24] ]
]

In this examle block is 2x2x3 size, but it can have XxYx3 size. I've tried different reshape() and transpose(), the order parameter, but nothing helps

Upvotes: 0

Views: 308

Answers (1)

Massifox
Massifox

Reputation: 4487

It is not possible to do this in python, because numpy does not support jagged arrays natively. But you can work around this problem by adding null values ​​or using masked array to signal that some indices are invalid in some rows.

Here you can find an example to help clarify ideas.

Upvotes: 1

Related Questions