0

I have a numpy array as follows:

matrix = array([[0.],
       [1.],
       [2.],
       [3.],
       [4.]])

I have another array:

x = [0.05385944 0.05419472 0.05453447 0.05487901]

I would like to insert the second array into the first index of the first array such like:

matrix = array([[0. 0.05385944 0.05419472 0.05453447 0.05487901],
       [1.],
       [2.],
       [3.],
       [4.]])

I have tried

matrix = np.insert(matrix, 1, x), 

but this is creating an array as such:

array([ 0. 0.05385944, 0.05419472,  0.05453447,  0.05487901, 1., 2., 3., 4.])

1 Answer 1

2

Numpy does not support variable dimensions. So it cannot have a row that has more elements than others.

Regular Python lists can do it though:

matrix = [[0.],
       [1.],
       [2.],
       [3.],
       [4.]]

x = [0.05385944, 0.05419472, 0.05453447, 0.05487901]

matrix[0].extend(x)

for row in matrix:print(row)

[0.0, 0.05385944, 0.05419472, 0.05453447, 0.05487901]
[1.0]
[2.0]
[3.0]
[4.0]
Sign up to request clarification or add additional context in comments.

2 Comments

How would I create the original matrix without numpy? I am currently using d = range(5) matrix = np.column_stack([d])
matrix = [ [float(i)] for i in range(5) ]

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.