Your inverse operation can be split into 2 simplier operation:

  1. concatenate rows(numpy.vstack)
  2. concatenate columns(numpy.hstack)

So, if you have matrix divided into 4 submatrix:

M = |m1|m2| 
    |m3|m4|

then M = hstack(vstack(m1, m2), vstack(m3, m4).

This operations can be code like this:

import itertools
import math

# iterators
def ihstack(*matrixes):
    return map(lambda rows: itertools.chain(*rows), zip(*matrixes))

def ivstack(*matrixes):
    return itertools.chain(*matrixes)

# main function
def squarejoin(*matrixes):
    size = int(math.sqrt(len(matrixes)))
    assert size ** 2 == len(matrixes), 'Incorrect number of matrices'
    return _matrixjoin(matrixes, size, size)

def _matrixjoin(matrixes, hsize, vsize):
    print(matrixes, hsize, vsize)
    return ivstack(*(ihstack(*itertools.islice(matrixes, i*hsize, (i+1)*hsize)) for i in range(vsize)))
Answer from kammala on Stack Overflow
Top answer
1 of 2
1

Your inverse operation can be split into 2 simplier operation:

  1. concatenate rows(numpy.vstack)
  2. concatenate columns(numpy.hstack)

So, if you have matrix divided into 4 submatrix:

M = |m1|m2| 
    |m3|m4|

then M = hstack(vstack(m1, m2), vstack(m3, m4).

This operations can be code like this:

import itertools
import math

# iterators
def ihstack(*matrixes):
    return map(lambda rows: itertools.chain(*rows), zip(*matrixes))

def ivstack(*matrixes):
    return itertools.chain(*matrixes)

# main function
def squarejoin(*matrixes):
    size = int(math.sqrt(len(matrixes)))
    assert size ** 2 == len(matrixes), 'Incorrect number of matrices'
    return _matrixjoin(matrixes, size, size)

def _matrixjoin(matrixes, hsize, vsize):
    print(matrixes, hsize, vsize)
    return ivstack(*(ihstack(*itertools.islice(matrixes, i*hsize, (i+1)*hsize)) for i in range(vsize)))
2 of 2
0

Here I have an example program where a 2 loops implementation works and is crystal clear in its intent, a 1 loop implementation works and is, imho, slightly less clear and eventually a 0 (explicit, btw) loops implementation that, alas, is buggy.

My vote goes to the two loops... further, I'd like to be shown what's wrong with my 0 loops attempt

Code

import itertools

def pm(m):
    for row in m: print row

mat = []
n = 8
for i in range(n):
    mat.append(range(i*n, i*n+n))

# this is shorthand for your splitmat function
res = map(lambda (x,y):
          map(lambda z:z[y[0]:y[1]],mat[x[0]:x[1]]),
          itertools.product([(0,n/2),(n/2,n)],repeat=2))
pm(res)

print "\n2 cycles"
mat = []
for i, j in ((0,1),(2,3)):
    for a, b in zip(res[i],res[j]):
        mat.append(a+b)
pm(mat)

print "\n1 cycle"
mat = []
for i, j in ((0,1),(2,3)):
    map(lambda x: mat.append(x[0]+x[1]), zip(res[i],res[j]))
pm(mat)

print "\n0 cycles"
mat =  map(lambda i_j: 
       map(lambda x: x[0]+x[1], zip(res[i_j[0]],res[i_j[1]])), ((0,1),(2,3)))
pm(mat)

Output

[[0, 1, 2, 3], [8, 9, 10, 11], [16, 17, 18, 19], [24, 25, 26, 27]]
[[4, 5, 6, 7], [12, 13, 14, 15], [20, 21, 22, 23], [28, 29, 30, 31]]
[[32, 33, 34, 35], [40, 41, 42, 43], [48, 49, 50, 51], [56, 57, 58, 59]]
[[36, 37, 38, 39], [44, 45, 46, 47], [52, 53, 54, 55], [60, 61, 62, 63]]

2 cicli
[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, 27, 28, 29, 30, 31]
[32, 33, 34, 35, 36, 37, 38, 39]
[40, 41, 42, 43, 44, 45, 46, 47]
[48, 49, 50, 51, 52, 53, 54, 55]
[56, 57, 58, 59, 60, 61, 62, 63]

1 ciclo
[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, 27, 28, 29, 30, 31]
[32, 33, 34, 35, 36, 37, 38, 39]
[40, 41, 42, 43, 44, 45, 46, 47]
[48, 49, 50, 51, 52, 53, 54, 55]
[56, 57, 58, 59, 60, 61, 62, 63]

0 cicli
[[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, 27, 28, 29, 30, 31]]
[[32, 33, 34, 35, 36, 37, 38, 39], [40, 41, 42, 43, 44, 45, 46, 47], [48, 49, 50, 51, 52, 53, 54, 55], [56, 57, 58, 59, 60, 61, 62, 63]]
🌐
DigitalOcean
digitalocean.com › community › tutorials › concatenate-lists-python
6+ Ways to Concatenate Lists in Python | DigitalOcean
Learn to concatenate lists in Python with examples, pros/cons, and performance tips. Explore Python list concatenation arrays without NumPy.
Discussions

Python arrays without numpy!
Can someone help me regarding the subtraction and multiplication of two matrices which I created using arrays (without numpy) and I am doing it using object oriented by making class and functions. I had created 2 matrices and print them by calling the class in objects and now I have to make ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
2
0
November 5, 2020
How do i combine multiple numpy arrays into one?
np.stack(), np.vstack(), np.hstack(), np.dstack(), np.concatenate(), np.column_stack(), np.row_stack(). One of those might work for your case. Just check out the examples for what might be analogous to your problem. Probably a horizontal stack if this is a 1D array (error message). Check out the left menu: https://numpy.org/doc/stable/reference/generated/numpy.stack.html#numpy.stack More on reddit.com
🌐 r/learnpython
9
3
September 15, 2023
Anyone know how to join two arrays together? - Arcade - Microsoft MakeCode
I need to join multiple arrays together as one array. I can make an array with both arrays as its contents, but thats not what I need. Anyone know? More on forum.makecode.com
🌐 forum.makecode.com
1
January 30, 2023
python - Concatenate Numpy arrays without copying - Stack Overflow
If you want you can allocate more space than needed and it will not take up more RAM because of the way numpy works. ... The memory is used only once data is put into the array. Creating a new array from concatenating two will never finish on a dataset of any size, i.e. More on stackoverflow.com
🌐 stackoverflow.com
🌐
TutorialsPoint
tutorialspoint.com › python-program-to-concatenate-two-arrays
Python - Join Arrays
May 5, 2023 - In this approach, we first convert arrays to list objects, then concatenate the lists using the + operator and convert back to get merged array.
🌐
Python Guides
pythonguides.com › python-concatenate-arrays
How to Concatenate Arrays in Python
August 20, 2025 - Finally, Python’s itertools module has a chain() function that works like concatenation. from itertools import chain arr1 = [1, 2, 3] arr2 = [4, 5, 6] arr3 = [7, 8, 9] result = list(chain(arr1, arr2, arr3)) print(result) ... I use this when I want to concatenate multiple arrays without creating intermediate lists. If you’re working with small lists, use + or extend(). If you need performance with numbers, go with NumPy (concatenate, hstack, vstack).
🌐
Awkward-array
awkward-array.org › doc › main › user-guide › how-to-restructure-concatenate.html
How to concatenate and interleave arrays — Awkward Array 2.13.0 documentation
It does not refer to adding fields ... pandas.concat does both, depending on its axis argument (and there’s no equivalent in NumPy). Here’s a table-like example of concatenation in Awkward Array:...
🌐
Python Data Science Handbook
jakevdp.github.io › PythonDataScienceHandbook › 03.06-concat-and-append.html
Combining Datasets: Concat and Append | Python Data Science Handbook
Because direct array concatenation is so common, Series and DataFrame objects have an append method that can accomplish the same thing in fewer keystrokes. For example, rather than calling pd.concat([df1, df2]), you can simply call df1.append(df2): ... Keep in mind that unlike the append() and extend() methods of Python lists, the append() method in Pandas does not modify the original object–instead it creates a new object with the combined data.
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
Python arrays without numpy! - Curriculum Help - The freeCodeCamp Forum
November 5, 2020 - Can someone help me regarding the subtraction and multiplication of two matrices which I created using arrays (without numpy) and I am doing it using object oriented by making class and functions. I had created 2 matrices and print them by calling the class in objects and now I have to make ...
Find elsewhere
🌐
Statology
statology.org › home › how to concatenate arrays in python (with examples)
How to Concatenate Arrays in Python (With Examples)
March 11, 2021 - This tutorial explains how to concatenate arrays in Python, including several examples.
🌐
Quora
quora.com › How-do-you-add-two-arrays-together-in-Python
How to add two arrays together in Python - Quora
Answer (1 of 2): There are two methods we can use to add two arrays. Either the "+" operator or the numpy.add() function can be used. I'll demonstrate how to add using both approaches. 1.Using “+” Operator In this approach, two distinct arrays are declared, and then they are added by ...
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.concatenate.html
numpy.concatenate — NumPy v2.5 Manual
The axis along which the arrays will be joined. If axis is None, arrays are flattened before use. Default is 0. ... If provided, the destination to place the result. The shape must be correct, matching that of what concatenate would have returned if no out argument were specified.
🌐
CSDN
devpress.csdn.net › python › 62fd0fbe7e668234661914b7.html
Concatenate Numpy arrays without copying_python_Mangs-Python
August 17, 2022 - If you know beforehand how many arrays you need, you can instead start with one big array that you allocate beforehand, and have each of the small arrays be a view to the big array (e.g. obtained by slicing). ... 问题:如何重塑熊猫。系列 在我看来,它就像 pandas.Series 中的一个错误。 a = pd.Series([1,2,3,4]) b = a.reshape(2,2) b b 有类型 Series 但无法显示,最后一条语句给出异常,非常冗长,最后一行是“TypeError: %d format: a number is required, not numpy.ndarray”。 b.sha
🌐
Medium
medium.com › @eapmartins › concatenation-of-array-ffa238f566b7
Concatenation of Array. How can I concatenate an array | by Alan Martins | Medium
June 21, 2025 - This problem is a simple way to get comfortable working with arrays. The task is to concatenate an array with itself, meaning the output should contain the original array followed by a duplicate of the same array.
🌐
Microsoft MakeCode
forum.makecode.com › arcade
Anyone know how to join two arrays together? - Arcade - Microsoft MakeCode
January 30, 2023 - I need to join multiple arrays together as one array. I can make an array with both arrays as its contents, but thats not what I need. Anyone know?
🌐
YouTube
youtube.com › how to fix your computer
PYTHON : Concatenate Numpy arrays without copying - YouTube
PYTHON : Concatenate Numpy arrays without copying [ Gift : Animated Search Engine : https://www.hows.tech/p/recommended.html ] PYTHON : Concatenate Numpy ar...
Published: December 6, 2021
Views: 31
🌐
iO Flood
ioflood.com › blog › numpy-concatenate
Numpy Concatenate: Mastering Array Joining in Python
January 30, 2024 - While numpy.concatenate() is powerful, it’s important to note that all input arrays must have the same shape, except in the dimension corresponding to the axis (default is 0). If the arrays do not meet this requirement, you will encounter a ValueError. As you advance in your Python journey, you’ll often find yourself working with multidimensional arrays.
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.concatenate.html
numpy.concatenate — NumPy v2.6.dev0 Manual
Concatenate function that preserves input masks. ... Split an array into multiple sub-arrays of equal or near-equal size.
🌐
Arab Psychology
scales.arabpsychology.com › home › how to concatenate arrays in python (with examples)
How To Concatenate Arrays In Python (With Examples)
December 7, 2025 - How do you concatenate arrays in Python, and what are some examples? How can I create an array of arrays in Python? Can you provide some examples? How to Combine Arrays Horizontally in Python Like R’s cbind Using NumPy