0

In numpy, if I have a vector of zeros like this

vec1 = np.array([0., 0., 0., 0., 0., 0., 0.])

and another one like this vec2 = np.array([1.,2.,3.]), which is the quickest way to obtain

vec3 = np.array([1., 2., 3., 0., 0., 0., 0.])

(Basicaly I want the original vector whose initial entries are filled with the ones of the second vector).

1
  • 3
    vec1[:len(vec2)] = vec2 Commented Apr 8, 2022 at 17:01

1 Answer 1

1

It depends if you want to create a new array or modify in place.

New vec3 array (without altering vec1 and vec2):

vec3 = np.concatenate([vec2, vec1[vec2.shape[0]:]])

or

vec3 = np.r_[vec2, vec1[vec2.shape[0]:]]

Modifying vec1 in place:

vec1[:vec2.shape[0]] = vec2

used input:

vec1 = np.array([0., 0., 0., 0., 0., 0., 0.])
vec2 = np.array([1.,2.,3.])
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.