在python中更改4D ndarray的形状[已解决]

问题描述 投票:0回答:1

我正在使用此功能为4d矩阵重新编制索引。

def insideout(ndimage):
    len_outrow = len(ndimage)
    len_outcol = len(ndimage[0])
    len_inrow = len(ndimage[0][0])
    len_incol = len(ndimage[0][0][0])

    reversed_image = np.empty((len_inrow,len_incol,len_outrow,len_outcol))
    for outrow in range(len_outrow):
        for outcol in range(len_outcol):
            for inrow in range(len_inrow):
                for incol in range(len_incol):     
                    #reverse the indices
                    reversed_image[inrow][incol][outrow][outcol]=image[outrow][outcol][inrow][incol]
    return reversed_image

这对于326x326x43x25矩阵效果很好。它成功地将矩阵的形状更改为(43,25,236,236)。

我在不改变其形状的矩阵上进行了一些计算,现在我想使用相同的函数将其还原。

但是这次我收到以下错误

--------------------------------------------------------------------------- IndexError                                Traceback (most recent call
last) <ipython-input-18-da92ca90d937> in <module>
----> 1 insideout(reversed_image)

<ipython-input-17-4edf23c1ed04> in insideout(ndimage)
     17                 for incol in range(len_incol):
     18                     #reverse the indices
---> 19                     reversed_image[inrow][incol][outrow][outcol]=image[outrow][outcol][inrow][incol]
     20     return reversed_image

IndexError: index 25 is out of bounds for axis 0 with size 25

我不知道错误是从哪里来的!为什么有索引25(因为我已经在for循环内使用过range()了?)>

输入矩阵是一个4D ndarray

我正在使用此功能为4d矩阵重新编制索引。 def insideout(ndimage):len_outrow = len(ndimage)len_outcol = len(ndimage [0])len_inrow = len(ndimage [0] [0])len_incol = len(ndimage [...

python numpy multidimensional-array numpy-ndarray
1个回答
0
投票

您的代码中有错字,

© www.soinside.com 2019 - 2024. All rights reserved.