user3601754
user3601754

Reputation: 3862

How to reshape 2D arrays?

I would like to reshape four 2D arrays : A, B, C and D (i have split before a "big array" in order to minimize function...and these arrays are the analytical results of the minimization) in a specific order :

A B

C D

I try with np.reshape or vectorize then concatenate but impossible to get this order as you can see of the picture below, all is mixed. I should have a result homogeneous

enter image description here

Thanks for answer, it works well by this way!

And i must apply that on a great number of subarrays, so i would like to automize the reshape of the array, as example below, the case of 4 arrays. As you can see i try with loops for but it doesnt work and perhaps by this way it could be not very fast...

test_reshape = np.empty([20,20])


test_reshape[0:10,0:10] = frametemperature[0,:,:]
test_reshape[0:10,10:10*2.] = frametemperature[1,:,:]

test_reshape[10:10*2.,0:10] = frametemperature[2,:,:]
test_reshape[10:10*2.,10:10*2.] = frametemperature[3,:,:]



for i in range(frametemperature.shape[0]/2):
    for j in range(frametemperature.shape[0]/2):
        for k in range(frametemperature.shape[0]): 
            test_reshape[i*10:10*(i+1),j*10:10*(j+1)] = frametemperature[k,:,:]

Upvotes: 0

Views: 256

Answers (1)

cs_alumnus
cs_alumnus

Reputation: 1649

So you've got 4 2d arrays that you want to combine back into one 2d array.

1) create a empty 2d array to store them in

import numpy as np
blank = np.empty([4,4])

2) assign the arrays according to their location instead of concatenating

A = np.ones([2,2])
B = np.ones([2,2]) * 2
C = np.ones([2,2]) * 3
D = np.ones([2,2]) * 4
blank[0:2,0:2] = a
blank[0:2,2:4] = b
blank[2:4,0:2] = c
blank[2:4,2:4] = d
blank

Upvotes: 1

Related Questions