1

I would like to put data of a numpy array of any size from the start of another bigger array of zeros.

Looking at numpy documentation I found the function np.put but it gives me a problem like this:

Supossing

import numpy as np
b = np.zeros(5)
a = np.range(1,4)
np.put(b,a,a)

produces something in b like

[0,1,2,3,0]

I also tried to use place function

np.place(b,b>len(a),a)

but nothing changes the matrix.

[0,0,0,0,0]

If someone has already worked on this, his/her help would be really good now.

1
  • 1
    It's a bit difficult to understand the question, what is the behaviour you expect, and which command fails to do that? Commented Jun 8, 2016 at 16:34

1 Answer 1

1

You're using np.put incorrectly.

np.put(b, a, a)
#      ^  ^  ^
#      |  |  |
# Target  |  |
#   Indices  |
#       Values

You told np.put to place the values of a at positions defined by a.

Instead:

np.put(b, np.arange(len(a)), a)

or:

b[:len(a)] = a
Sign up to request clarification or add additional context in comments.

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.