简体   繁体   English

如何在 numpy 数组中插入

[英]How to insert in numpy array

There is Two matrices.有两个矩阵。 The first one is data matrix with dimension of (85,7794,64) and the second one is flashing with dimension of (85,7794).第一个是维度为(85,7794,64)的数据矩阵,第二个是维度为(85,7794)的闪烁。 I must extract some parts of data according to flashing matrix under a specific condition which is mentioned in below code.我必须在下面代码中提到的特定条件下根据闪烁矩阵提取部分数据。 There is no doubt about condition performance, but when i try to insert the extracted data (with dimension of (20,64)) to predefined storage matrix with dimension of (85,3600,64)(3600 because 180 parts of data with dimension of (20,64) should be extracted from data, so 180*20 = 3600 ) it gives an error which is mentioned below.条件性能毫无疑问,但是当我尝试将提取的数据(维度为(20,64))插入到维度为(85,3600,64)(3600)的预定义存储矩阵时,因为有 180 个维度的数据的 (20,64) 应该从数据中提取,所以 180*20 = 3600 ) 它给出了下面提到的错误。 Thanks if anyone can helps to solve it.谢谢,如果有人可以帮助解决它。

ERROR:错误:

ValueError: could not broadcast input array from shape (20,64) into shape (0,64) ValueError:无法将输入数组从形状(20,64)广播到形状(0,64)

storage_matrix = np.zeros((85,3600,64))


for i in range(0,84):    
    for j in range(0,7793):
        t = j + 1
        s = j + 20
        if Flashing[i,j] == 0 and Flashing[i,t] == 1:
            storage_matrix[i,j:s,:] = data[i,j:s,:]
            

Your range for the inner loop is too big.您的内循环范围太大。 Consider what happens when j>3600 in the inner loop.考虑在内循环中当j>3600时会发生什么。

When you say storage_matrix[i, j:s, :] , and if j>3600 , the code breaks because the size of the middle dimension of storage_matrix is 3600. That returns an empty array.当您说storage_matrix[i, j:s, :]并且如果j>3600时,代码会中断,因为storage_matrix的中间维度的大小是 3600。这将返回一个空数组。 Hence you are seeing a zero dimension in your error message.因此,您在错误消息中看到零维度。

Not sure what you are trying to do here but maybe you meant 3600 in your inner loop.不确定您在这里尝试做什么,但也许您的意思是 3600 在您的内部循环中。

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

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