簡體   English   中英

將3D numpy數組的參數傳遞到4D numpy數組

[英]Pass arguments of 3D numpy array into a 4D numpy array

我想根據在該數組的簡化3D版本上評估的numpy.argsort的結果對4D numpy數組進行排序。 像這樣:

array.shape
    (7, 3178, 3178, 3)
array_reduced.shape
    (7, 3178, 3178)
args=numpy.argsort(array_reduced,axis=0)
array_sorted=array[args,:]

這將返回一個內存錯誤:

    ---------------------------------------------------------------------------
    MemoryError                               Traceback (most recent call last)
    <ipython-input-48-04f432d9e05d> in <module>()
          ----> 1 array_sorted=array[args,:]

    MemoryError:

這可能是關於如何轉換lambda函數的愚蠢錯誤,但是如果有人可以幫助我,我將不勝感激!

--------------------------------------編輯----------- ------------

這段代碼做了我想要的事情,但是它很慢:

array_sorted=np.zeros(array.shape,dtype=np.uint8)
for thet in range (0, array.shape[0]):
    print(thet)
    for y in range (0, array.shape[1]):
        for x in range (0, array.shape[2]):
            array_sorted[thet,y,x,0]=(array[args[thet,y,x],y,x,0])
            array_sorted[thet,y,x,1]=(array[args[thet,y,x],y,x,1])
            array_sorted[thet,y,x,2]=(array[args[thet,y,x],y,x,2])

您可以使用np.ogrid將循環向量化:

i,j,k = np.ogrid[tuple(map(slice, array.shape[:-1]))]
array_sorted = array[args, j, k]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM