How To Construct 2d Array From 2d Arrays
I need to reconstruct 2d array(from image colour channel) of the specific shape from a number of smaller arrays: import numpy as np from PIL import Image def blockshaped(arr, nrow
Solution 1:
Reshape to split the first axis into three axes of lengths 2,2,8
, keeping the second axis as it is. Then, permute axes with rollaxis
/swapaxes
/transpose
and a final reshape to have 16 x 16
shape -
n_a2.reshape(2,2,8,8).swapaxes(1,2).reshape(16,16)
Sample run for verification -
In [46]: c = np.random.randint(11,99,(16,16))
In [47]: n_a = blockshaped(c,8,8) # dividing array into 4 subarrays
...: n_a2 = np.concatenate(n_a, axis = 0) #concatenate arrays
...:
In [48]: out = n_a2.reshape(2,2,8,8).swapaxes(1,2).reshape(16,16)
In [49]: np.allclose(out, c)
Out[49]: True
Post a Comment for "How To Construct 2d Array From 2d Arrays"