【问题标题】:How to iterate over the first index of 4D matrix every nth element?如何每隔 n 个元素迭代 4D 矩阵的第一个索引?
【发布时间】:2021-12-14 03:09:12
【问题描述】:

假设有一个 4D 矩阵 a=(20,3,100,100)。我想将a 的第一个索引(即20)划分为for 循环中的4 批次,然后将每次迭代附加到输出中。我不想使用 reshape,因为每个批次都会被输入到网络中。

输出的大小应该是(4,5,3,100,100)

这是我的尝试:

output=[]
a=np.random.randint(0,100,size=(20,3,600,600))
for i in a[1:20:5,:,:,:] :  
            cnn_in=a[i,:,:,:]
            output.append(cnn_in)  

【问题讨论】:

    标签: python list for-loop indexing


    【解决方案1】:

    这样的?

    for i in range(0,20,5):
         cnn_in=a[i:i+5]
         output.append(cnn_in)  
    

    或更简单的np.splitoutput=np.split(a,4)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-14
      • 2023-01-11
      • 2020-09-06
      • 2021-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-25
      相关资源
      最近更新 更多