Skip to content
Advertisement

How do I merge several 2D arrays into a single 2D array (list) in a box-like manner in python?

Say I have 9 2D arrays in the following format:

JavaScript

and I want to concatenate them to get a 2D array like this:

JavaScript

where

JavaScript

I will have N arrays in my case and I calculate the value of N like so:

JavaScript

So essentially, I had an image that was broken down into N tiles and after some processing, I have these image tile data stored as numpy arrays and I want to concatenate/merge them into a single 2D numpy array in the same orientation as the original image. How can I do that?

Advertisement

Answer

This seems a perfect use case for bmat

Edit: How to use bmat

bmat accepts as the first argument a matrix of blocks.

JavaScript

And is not limited to the 9 submatrices case, it was a coincidence that in the bmat documentation their example is the same size as the example in your question.

JavaScript

Gives

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement