Suppose I have a matrix
mat = np.empty((0, 3, 5))
and I have a another matrix of shape (3, 5), how can I add that matrix to mat[0]?
I've tried different combinations of stack, vstack, hstack, concatenate and insert and they don't seem to work
Suppose I have a matrix
mat = np.empty((0, 3, 5))
and I have a another matrix of shape (3, 5), how can I add that matrix to mat[0]?
I've tried different combinations of stack, vstack, hstack, concatenate and insert and they don't seem to work
I suggest you to do this to create your matrix. :)
Your matrix is empty in your current state. Like []
.
import numpy as np
rows = 5
cols = 3
mat = np.array([[0] * cols] * rows)
mat_2 = np.empty((0, 3, 5))
print mat
print mat_2
# out for mat:
[[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]]
# out for mat_2:
[]
To make my answer final, as I stated in comments, just add the two matrices you want to add like m1 + m2
.
rows = 5
cols = 3
mat = np.array([[1] * cols] * rows)
mat_2 = np.array([[2] * cols] * rows)
print mat + mat_2
# out :
[[3 3 3]
[3 3 3]
[3 3 3]
[3 3 3]
[3 3 3]]
I understand that you want to insert a 3x5 matrix to an empty array. Then np.append
can do that. Just specify the right axis.
import numpy as np
mat = np.empty((0,3,5))
mat3x5 = np.matrix('1,1,1,1,2;3,1,2,3,1;2,3,1,2,3')
mat3x5 = np.expand_dims(mat3x5, 0)
result = np.append(mat, mat3x5, axis=0)