分配给多维数组 [英] assigning to multi-dimensional array

查看:138
本文介绍了分配给多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有零的三维阵列,我想用一维数组来填补它:

 在[136]:C = np.zeros((3,5,6),DTYPE = INT)在[137]:C
出[137]:
阵列([[[0,0,0,0,0,0],
        [0,0,0,0,0,0],
        [0,0,0,0,0,0],
        [0,0,0,0,0,0],
        [0,0,0,0,0,0],       [0,0,0,0,0,0],
        [0,0,0,0,0,0],
        [0,0,0,0,0,0],
        [0,0,0,0,0,0],
        [0,0,0,0,0,0],       [0,0,0,0,0,0],
        [0,0,0,0,0,0],
        [0,0,0,0,0,0],
        [0,0,0,0,0,0],
        [0,0,0,0,0,0]]])在[138]:■
出[138]:阵列([10,20,30,40,50])

我要做到这一点:(不使用循环)

 阵列([[[10,10,10,10,10,10],
        [20,20,20,20,20,20],
        [30,30,30,30,30,30],
        [40,40,40,40,40,40],
        [50,50,50,50,50,50]]       [10,10,10,10,10,10],
        [20,20,20,20,20,20],
        [30,30,30,30,30,30],
        [40,40,40,40,40,40],
        [50,50,50,50,50,50]]       [10,10,10,10,10,10],
        [20,20,20,20,20,20],
        [30,30,30,30,30,30],
        [40,40,40,40,40,40],
        [50,50,50,50,50,50]]])

通过分配s到每个第i个元素的每列

请注意,我可以很容易地得到类似的东西:

 阵列([[[10,20,30,40,50,60],
        [10,20,30,40,50,60],
        [10,20,30,40,50,60],
        [10,20,30,40,50,60],
        [10,20,30,40,50,60]]       [10,20,30,40,50,60],
        [10,20,30,40,50,60],
        [10,20,30,40,50,60],
        [10,20,30,40,50,60],
        [10,20,30,40,50,60]]       [10,20,30,40,50,60],
        [10,20,30,40,50,60],
        [10,20,30,40,50,60],
        [10,20,30,40,50,60],
        [10,20,30,40,50,60]]])

方式:

  C [:,:,:] = S

但看不到如何送分配到j对所有的i和K [I,J,K]

似乎numpy的优先考虑的最后一个冒号C [:,:,:]。有没有解决这个的好办法?


解决方案

  TMP = C.swapaxes(1,2)
TMP [:] = S
C = tmp.swapaxes(1,2)

i have a 3d array of zeros and i want to fill it with a 1d array:

In [136]: C = np.zeros((3,5,6),dtype=int)

In [137]: C
Out[137]: 
array([[[0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0]],

       [[0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0]],

       [[0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0]]])

In [138]: s
Out[138]: array([10, 20, 30, 40, 50])

I want to achieve this: (without using a loop)

array([[[10, 10, 10, 10, 10, 10],
        [20, 20, 20, 20, 20, 20],
        [30, 30, 30, 30, 30, 30],
        [40, 40, 40, 40, 40, 40],
        [50, 50, 50, 50, 50, 50]],

       [[10, 10, 10, 10, 10, 10],
        [20, 20, 20, 20, 20, 20],
        [30, 30, 30, 30, 30, 30],
        [40, 40, 40, 40, 40, 40],
        [50, 50, 50, 50, 50, 50]],

       [[10, 10, 10, 10, 10, 10],
        [20, 20, 20, 20, 20, 20],
        [30, 30, 30, 30, 30, 30],
        [40, 40, 40, 40, 40, 40],
        [50, 50, 50, 50, 50, 50]]])

by assigning s to each column of each ith element.

note I can easily get something similar:

array([[[10, 20, 30, 40, 50, 60],
        [10, 20, 30, 40, 50, 60],
        [10, 20, 30, 40, 50, 60],
        [10, 20, 30, 40, 50, 60],
        [10, 20, 30, 40, 50, 60]],

       [[10, 20, 30, 40, 50, 60],
        [10, 20, 30, 40, 50, 60],
        [10, 20, 30, 40, 50, 60],
        [10, 20, 30, 40, 50, 60],
        [10, 20, 30, 40, 50, 60]],

       [[10, 20, 30, 40, 50, 60],
        [10, 20, 30, 40, 50, 60],
        [10, 20, 30, 40, 50, 60],
        [10, 20, 30, 40, 50, 60],
        [10, 20, 30, 40, 50, 60]]])

By :

 C[:,:,:] = s

But can't see how to assign s to j for all i and k in [i,j,k]

it seems numpy prioritises the last colon C[:,:,:]. is there a nice way around this?

解决方案

tmp = C.swapaxes(1, 2)
tmp[:] = s
C = tmp.swapaxes(1, 2)

这篇关于分配给多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆