0

Suppose I have a numpy array array of matrices

A=numpy.array([[[1, 0, 0],[1, 1, 0],[0, 1, 0],[1, 1, 1]],[[1, 0, 0],[1, 1, 1],[0, 0, 1],[1, 0, 1]]])

and another array

B=np.array([[1,2,3,4],[5,6,7,8]])

which we interpret as an array of columns to insert into A.

How can I now insert the columns from B into A, i.e. to obtain

[[[1, 1, 0, 0],[2, 1, 1, 0],[3, 0, 1, 0],[4, 1, 1, 1]],[[5, 1, 0, 0],[6, 1, 1, 1],[7, 0, 0, 1],[8, 1, 0, 1]]]

?

I tried to use numpy.insert, but unfortunately I did not find to solve this problem with this method.

3 Answers 3

1

You can use np.concatenate to join A along the last axis (axis=2) - which represents the columns in each matrix

import numpy as np


A = np.array([[[1, 0, 0],[1, 1, 0],[0, 1, 0],[1, 1, 1]],
              [[1, 0, 0],[1, 1, 1],[0, 0, 1],[1, 0, 1]]])
B = np.array([[1,2,3,4],[5,6,7,8]])


B_reshaped = B.reshape(A.shape[0], A.shape[1], 1)


result = np.concatenate([B_reshaped, A], axis=2)

print(result)

Result

[[[1, 1, 0, 0],[2, 1, 1, 0],[3, 0, 1, 0],[4, 1, 1, 1]],
 [[5, 1, 0, 0],[6, 1, 1, 1],[7, 0, 0, 1],[8, 1, 0, 1]]]
Sign up to request clarification or add additional context in comments.

2 Comments

It might be helpful to include what your code does.
iam typing.....
1

You could add a dimension to B and concatenate:

out = np.concatenate([B[..., None], A], axis=2)

Generic approach for an arbitrary number of dimensions (A having n dimensions and B having n-1):

out = np.concatenate([B[..., None], A], axis=-1)

Using insert:

out = np.insert(A, 0, B, axis=2)

Output:

array([[[1, 1, 0, 0],
        [2, 1, 1, 0],
        [3, 0, 1, 0],
        [4, 1, 1, 1]],

       [[5, 1, 0, 0],
        [6, 1, 1, 1],
        [7, 0, 0, 1],
        [8, 1, 0, 1]]])

Comments

0

This may save a bit of memory and improve performance :

import numpy as np

# Arrays A and B
A = np.array([[[1, 0, 0], [1, 1, 0], [0, 1, 0], [1, 1, 1]], 
              [[1, 0, 0], [1, 1, 1], [0, 0, 1], [1, 0, 1]]])

B = np.array([[1, 2, 3, 4], 
              [5, 6, 7, 8]])

res = np.concatenate([B[:,:,None],A],axis=2)
print(res)
'''
[[[1 1 0 0]
  [2 1 1 0]
  [3 0 1 0]
  [4 1 1 1]]

 [[5 1 0 0]
  [6 1 1 1]
  [7 0 0 1]
  [8 1 0 1]]]

'''

If the dataset doesn't fit into memory, we can use memory-mapped arrays. This avoids loading the entire array into RAM at once.

import numpy as np

# Arrays A and B
A = np.array([[[1, 0, 0], [1, 1, 0], [0, 1, 0], [1, 1, 1]], 
              [[1, 0, 0], [1, 1, 1], [0, 0, 1], [1, 0, 1]]])

B = np.array([[1, 2, 3, 4], 
              [5, 6, 7, 8]])

n_rows, n_cols, n_depth = A.shape

res = np.memmap('res.dat', dtype='int', mode='w+', shape=(n_rows, n_cols, n_depth + 1))
res[:, :, :1] = B[:, :, None]# Insert B as the first column
res[:, :, 1:] = A # Add the rest of A's columns
print(res)

'''
[[[1 1 0 0]
  [2 1 1 0]
  [3 0 1 0]
  [4 1 1 1]]

 [[5 1 0 0]
  [6 1 1 1]
  [7 0 0 1]
  [8 1 0 1]]]
'''

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.