#### [SOLVED] Edit every other item in an array

``````matrix = np.array([[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]])
vector = np.array([0,0,0,0])
``````

For vectors, you can edit every other element like so

``````vector[1::2] = 1
``````

This gives

``````np.array([0,1,0,1])
``````

However;

``````matrix[1::2] = 1
``````

yields

``````np.array([[0,0,0,0],[1,1,1,1],[0,0,0,0],[1,1,1,1]])
``````

I would like the output

``````np.array([[0,1,0,1],[0,1,0,1],[0,1,0,1],[0,1,0,1]])
``````

There is a brute force approach to take the shape of the array, flatten it, use [1::2], and reshape, but i'm sure there is a more elegant solution i am missing.

Any help would be appreciated.

#### @CoryKramer 2020-01-14 13:11:35

You can do something similar with multidimensional indexing

``````>>> matrix
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]])
>>> matrix[:,1::2] = 1
>>> matrix
array([[0, 1, 0, 1],
[0, 1, 0, 1],
[0, 1, 0, 1],
[0, 1, 0, 1]])
``````

### [SOLVED] How do I remove a particular element from an array in JavaScript?

• 2011-04-23 22:17:18
• Walker
• 6260204 View
• 7817 Score
• Tags:   javascript arrays

### [SOLVED] Finding the index of an item given a list containing it in Python

• 2008-10-07 01:39:38
• Eugene M
• 3603810 View
• 2958 Score
• Tags:   python list indexing