简体   繁体   English

如何重塑2D阵列?

[英]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 : 我想重塑四个2D数组:A,B,C和D(为了最小化功能,我在“大数组”之前拆分了...这些数组是最小化的分析结果)按特定顺序排列:

AB AB

CD 光盘

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. 我尝试使用np.reshape或vectorize然后进行连接,但是无法获得此顺序,如您在下面的图片中所见,所有内容都是混合的。 I should have a result homogeneous 我应该得到均匀的结果

在此处输入图片说明

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. 而且我必须将其应用于大量的子数组,因此我想自动化数组的重塑,如下例所示(4个数组的情况)。 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,:,:]

So you've got 4 2d arrays that you want to combine back into one 2d array. 因此,您有4个2d阵列要合并回一个2d阵列。

1) create a empty 2d array to store them in 1) 创建一个空的2d数组将其存储在

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

2) assign the arrays according to their location instead of concatenating 2)根据其位置分配数组,而不是串联

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM