How to use numpy.dstack in a loop?

后端 未结 3 1848
礼貌的吻别
礼貌的吻别 2021-01-20 18:43

I\'m trying to populate an array by using 2D arrays with np.dstack.

m1 = np.array([[1,1],[1,1]])
m2 = np.array([[2,2],[2,2]])
m3 = np.array([[3,3],[3,3]])

         


        
相关标签:
3条回答
  • 2021-01-20 18:51

    As other answers have shown, you can put the arrays in a list, and join them into a new array with one call:

    In [173]: m1 = np.array([[1,1],[1,1]])
         ...: m2 = np.array([[2,2],[2,2]])
         ...: m3 = np.array([[3,3],[3,3]])
         ...: 
         ...: 
    In [174]: alist = [m1,m2,m3]
    In [175]: np.stack(alist)
    Out[175]: 
    array([[[1, 1],
            [1, 1]],
    
           [[2, 2],
            [2, 2]],
    
           [[3, 3],
            [3, 3]]])
    

    In numpy the first axis is the outer most. In contrast in MATLAB the last axis is outermost. So stack (or np.array(alist)) is the natural equivalent. But if you must join them on a new inner axis use:

    In [176]: np.stack(alist,axis=2)        # or np.dstack(alist)
    Out[176]: 
    array([[[1, 2, 3],
            [1, 2, 3]],
    
           [[1, 2, 3],
            [1, 2, 3]]])
    

    Or if you are creating the arrays one at a time, use a list append:

    In [177]: alist = []
    In [178]: for a in m1,m2,m3:
         ...:     alist.append(a)
    

    Perhaps the closest thing to your MATLAB is:

    In [181]: arr = np.zeros((2,2,3),int)
    In [183]: for i,m in enumerate([m1,m2,m3]):
         ...:     arr[:,:,i] = m
    
    for i=1:10 
        var = some2Dmatrix 
        A(:,:,i) = var;
    end
    

    In contrast to MATLAB numpy does not simply add a layer when you index beyond the current size. I also suspect that MATLAB code actually hides some costly matrix resizing code. It's not something I did years ago when I used MATLAB professionally.

    But to answer the question that we don't want to answer:

    In [185]: arr = np.zeros((2,2,0),int)   # your empty(?)
    In [187]: for m in (m1,m2,m3):
         ...:     arr = np.concatenate((arr, m[:,:,None]), axis=2)    
    In [188]: arr.shape
    Out[188]: (2, 2, 3)
    

    If you don't understand the details of this iteration, you probably shouldn't be trying to build a larger array with repeated concatenate (or dstack). You have to get the initial shape of arr correct, and you have to get the right shape of the iteration array right. Plus this is making brand new arr with each loop. That's costlier than list append.

    0 讨论(0)
  • 2021-01-20 18:52

    There is no need to do this in a for loop, you can add a variable number of matrices in the tuple you pass to np.dstack, like:

    >>> np.dstack((m1, m2, m3))
    array([[[1, 2, 3],
            [1, 2, 3]],
    
           [[1, 2, 3],
            [1, 2, 3]]])
    
    0 讨论(0)
  • 2021-01-20 19:11

    You do not need a loop. You need a list or a tuple of arrays:

    m1 = np.array([[1,1],[1,1]])
    m2 = np.array([[2,2],[2,2]])
    m3 = np.array([[3,3],[3,3]])
    
    arrays = m1, m2, m3 # A tuple of arrays
    lst = np.dstack(arrays) # Stack them all
    
    0 讨论(0)
提交回复
热议问题