Home > front end >  Cell wise concatenation of python arrays
Cell wise concatenation of python arrays

Time:09-21

I have multiple arrays of equal size, and I want to concatenate the values in each cell to produce an output array of the same size, but where each element is a list. How can I do this?

Example.

Input:
a = [[1,2],
     [3,4]]
b = [[5,6],
     [7,8]]

Desired output:
c = [[[1,5],[2,6]],
     [[3,7],[4,8]]]

How can I achieve this? My arrays are actually NumPy arrays, if that helps.

Followup Question: Concatenate Iteratively

The first answer suggests using np.dstack, which works for the above example.

How do I do the concatenation in an iterative fashion? After obtaining

c = [[[1,5],[2,6]],
     [[3,7],[4,8]]]

if I have

d = [[9,10],
     [11,12]]

I want to "concatenate" c and d to obtain

e = [[[1,5,9],[2,6,10]],
     [[3,7,11],[4,8,12]]]

CodePudding user response:

You can use np.dstack.

>>> a
array([[1, 2],
       [3, 4]])
>>> b
array([[5, 6],
       [7, 8]])
>>> np.dstack((a,b))
array([[[1, 5],
        [2, 6]],

       [[3, 7],
        [4, 8]]])

See help(np.dstack) for more information and examples

>>> help(np.dstack)
Help on function dstack in module numpy:

dstack(tup)
    Stack arrays in sequence depth wise (along third axis).

    This is equivalent to concatenation along the third axis after 2-D arrays
    of shape `(M,N)` have been reshaped to `(M,N,1)` and 1-D arrays of shape
    `(N,)` have been reshaped to `(1,N,1)`. Rebuilds arrays divided by
    `dsplit`.

    This function makes most sense for arrays with up to 3 dimensions. For
    instance, for pixel-data with a height (first axis), width (second axis),
    and r/g/b channels (third axis). The functions `concatenate`, `stack` and
    `block` provide more general stacking and concatenation operations.

    Parameters
    ----------
    tup : sequence of arrays
        The arrays must have the same shape along all but the third axis.
        1-D or 2-D arrays must have the same shape.

    Returns
    -------
    stacked : ndarray
        The array formed by stacking the given arrays, will be at least 3-D.

    Examples
    --------
    >>> a = np.array((1,2,3))
    >>> b = np.array((2,3,4))
    >>> np.dstack((a,b))
    array([[[1, 2],
            [2, 3],
            [3, 4]]])

    >>> a = np.array([[1],[2],[3]])
    >>> b = np.array([[2],[3],[4]])
    >>> np.dstack((a,b))
    array([[[1, 2]],
           [[2, 3]],
           [[3, 4]]])

CodePudding user response:

As mentioned, in other answers/comments np.dstack would be the right way to stack them.

However, if you want to iteratively add items (PART 2 of your question), you can simply modify the list of stacks that you want to np.dstack since it's only referencing the elements of this original list.

If this final object needs to be the same after each iteration, then you can simply overwrite the object with the next iteration's output.

inp = [a,b,d,a,b,d]
l = []

for i in inp:
    l.append(i)
    print(np.dstack(l))
    print('--------')
    
    
[[[1]
  [2]]

 [[3]
  [4]]]
-------
[[[1 5]
  [2 6]]

 [[3 7]
  [4 8]]]
-------
[[[ 1  5  9]
  [ 2  6 10]]

 [[ 3  7 11]
  [ 4  8 12]]]
-------
[[[ 1  5  9  1]
  [ 2  6 10  2]]

 [[ 3  7 11  3]
  [ 4  8 12  4]]]
-------
[[[ 1  5  9  1  5]
  [ 2  6 10  2  6]]

 [[ 3  7 11  3  7]
  [ 4  8 12  4  8]]]
-------
[[[ 1  5  9  1  5  9]
  [ 2  6 10  2  6 10]]

 [[ 3  7 11  3  7 11]
  [ 4  8 12  4  8 12]]]
-------
  • Related