Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to copy an array into a bigger array(partial copy)

I am trying to copy an array into another.

a = np.array([1]*3)
b = np.array([2]*2)

I tried copyto()

np.copyto(a,b)

But I get:

Traceback (most recent call last): File "", line 1, in np.copyto(a,b) ValueError: could not broadcast input array from shape (2) into shape (3)

How can I get a to become equal to [2,2,1] ?

like image 225
maazza Avatar asked Feb 02 '16 21:02

maazza


People also ask

How do I copy part of an array to another array?

The Array. Copy() method in C# is used to copy section of one array to another array. Array. Copy(src, dest, length);

How do you copy part of an array?

JavaScript Arrays Copy part of an ArrayThe slice() method returns a copy of a portion of an array.


1 Answers

Assign the values in b to a slice of a:

In [16]: a[:len(b)] = b

In [17]: a
Out[17]: array([2, 2, 1])
like image 92
unutbu Avatar answered Nov 07 '22 10:11

unutbu