NumPy - Insert an array of zeros after specified indices
up vote
5
down vote
favorite
My code is:
x=np.linspace(1,5,5)
a=np.insert(x,np.arange(1,5,1),np.zeros(3))
The output I want is:
[1,0,0,0,2,0,0,0,3,0,0,0,4,0,0,0,5]
The error I get is:
ValueError: shape mismatch: value array of shape (3,) could not be
broadcast to indexing result of shape (4,)
When I do:
x=np.linspace(1,5,5)
a=np.insert(x,np.arange(1,5,1),0)
The out is:
array([1., 0., 2., 0., 3., 0., 4., 0., 5.])
Why it doesn't work when I try to insert an array?
P.S. I I cannot use loops
python arrays numpy
New contributor
add a comment |
up vote
5
down vote
favorite
My code is:
x=np.linspace(1,5,5)
a=np.insert(x,np.arange(1,5,1),np.zeros(3))
The output I want is:
[1,0,0,0,2,0,0,0,3,0,0,0,4,0,0,0,5]
The error I get is:
ValueError: shape mismatch: value array of shape (3,) could not be
broadcast to indexing result of shape (4,)
When I do:
x=np.linspace(1,5,5)
a=np.insert(x,np.arange(1,5,1),0)
The out is:
array([1., 0., 2., 0., 3., 0., 4., 0., 5.])
Why it doesn't work when I try to insert an array?
P.S. I I cannot use loops
python arrays numpy
New contributor
add a comment |
up vote
5
down vote
favorite
up vote
5
down vote
favorite
My code is:
x=np.linspace(1,5,5)
a=np.insert(x,np.arange(1,5,1),np.zeros(3))
The output I want is:
[1,0,0,0,2,0,0,0,3,0,0,0,4,0,0,0,5]
The error I get is:
ValueError: shape mismatch: value array of shape (3,) could not be
broadcast to indexing result of shape (4,)
When I do:
x=np.linspace(1,5,5)
a=np.insert(x,np.arange(1,5,1),0)
The out is:
array([1., 0., 2., 0., 3., 0., 4., 0., 5.])
Why it doesn't work when I try to insert an array?
P.S. I I cannot use loops
python arrays numpy
New contributor
My code is:
x=np.linspace(1,5,5)
a=np.insert(x,np.arange(1,5,1),np.zeros(3))
The output I want is:
[1,0,0,0,2,0,0,0,3,0,0,0,4,0,0,0,5]
The error I get is:
ValueError: shape mismatch: value array of shape (3,) could not be
broadcast to indexing result of shape (4,)
When I do:
x=np.linspace(1,5,5)
a=np.insert(x,np.arange(1,5,1),0)
The out is:
array([1., 0., 2., 0., 3., 0., 4., 0., 5.])
Why it doesn't work when I try to insert an array?
P.S. I I cannot use loops
python arrays numpy
python arrays numpy
New contributor
New contributor
edited Nov 8 at 13:50
jpp
80.2k184695
80.2k184695
New contributor
asked Nov 8 at 13:01
Danjmp
283
283
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Another option:
np.hstack((x[:,None], np.zeros((5,3)))).flatten()[:-3]
gives:
array([ 1., 0., 0., 0., 2., 0., 0., 0., 3., 0., 0., 0., 4.,
0., 0., 0., 5.])
That is, pretend x
is a column vector and stack a 5x3 block of zeros to the right of it and then flatten.
add a comment |
up vote
3
down vote
You can use np.repeat
to feed repeated indices. For a 1d array, thhe obj
argument for np.insert
reference individual indices.
x = np.linspace(1, 5, 5)
a = np.insert(x, np.repeat(np.arange(1, 5, 1), 3), 0)
array([ 1., 0., 0., 0., 2., 0., 0., 0., 3., 0., 0., 0., 4.,
0., 0., 0., 5.])
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Another option:
np.hstack((x[:,None], np.zeros((5,3)))).flatten()[:-3]
gives:
array([ 1., 0., 0., 0., 2., 0., 0., 0., 3., 0., 0., 0., 4.,
0., 0., 0., 5.])
That is, pretend x
is a column vector and stack a 5x3 block of zeros to the right of it and then flatten.
add a comment |
up vote
2
down vote
accepted
Another option:
np.hstack((x[:,None], np.zeros((5,3)))).flatten()[:-3]
gives:
array([ 1., 0., 0., 0., 2., 0., 0., 0., 3., 0., 0., 0., 4.,
0., 0., 0., 5.])
That is, pretend x
is a column vector and stack a 5x3 block of zeros to the right of it and then flatten.
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Another option:
np.hstack((x[:,None], np.zeros((5,3)))).flatten()[:-3]
gives:
array([ 1., 0., 0., 0., 2., 0., 0., 0., 3., 0., 0., 0., 4.,
0., 0., 0., 5.])
That is, pretend x
is a column vector and stack a 5x3 block of zeros to the right of it and then flatten.
Another option:
np.hstack((x[:,None], np.zeros((5,3)))).flatten()[:-3]
gives:
array([ 1., 0., 0., 0., 2., 0., 0., 0., 3., 0., 0., 0., 4.,
0., 0., 0., 5.])
That is, pretend x
is a column vector and stack a 5x3 block of zeros to the right of it and then flatten.
answered Nov 8 at 13:19
xnx
15k43670
15k43670
add a comment |
add a comment |
up vote
3
down vote
You can use np.repeat
to feed repeated indices. For a 1d array, thhe obj
argument for np.insert
reference individual indices.
x = np.linspace(1, 5, 5)
a = np.insert(x, np.repeat(np.arange(1, 5, 1), 3), 0)
array([ 1., 0., 0., 0., 2., 0., 0., 0., 3., 0., 0., 0., 4.,
0., 0., 0., 5.])
add a comment |
up vote
3
down vote
You can use np.repeat
to feed repeated indices. For a 1d array, thhe obj
argument for np.insert
reference individual indices.
x = np.linspace(1, 5, 5)
a = np.insert(x, np.repeat(np.arange(1, 5, 1), 3), 0)
array([ 1., 0., 0., 0., 2., 0., 0., 0., 3., 0., 0., 0., 4.,
0., 0., 0., 5.])
add a comment |
up vote
3
down vote
up vote
3
down vote
You can use np.repeat
to feed repeated indices. For a 1d array, thhe obj
argument for np.insert
reference individual indices.
x = np.linspace(1, 5, 5)
a = np.insert(x, np.repeat(np.arange(1, 5, 1), 3), 0)
array([ 1., 0., 0., 0., 2., 0., 0., 0., 3., 0., 0., 0., 4.,
0., 0., 0., 5.])
You can use np.repeat
to feed repeated indices. For a 1d array, thhe obj
argument for np.insert
reference individual indices.
x = np.linspace(1, 5, 5)
a = np.insert(x, np.repeat(np.arange(1, 5, 1), 3), 0)
array([ 1., 0., 0., 0., 2., 0., 0., 0., 3., 0., 0., 0., 4.,
0., 0., 0., 5.])
edited Nov 8 at 14:25
answered Nov 8 at 13:16
jpp
80.2k184695
80.2k184695
add a comment |
add a comment |
Danjmp is a new contributor. Be nice, and check out our Code of Conduct.
Danjmp is a new contributor. Be nice, and check out our Code of Conduct.
Danjmp is a new contributor. Be nice, and check out our Code of Conduct.
Danjmp is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53208295%2fnumpy-insert-an-array-of-zeros-after-specified-indices%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password