Home > OS >  How access odd index elements and even index elements and merge them vertically
How access odd index elements and even index elements and merge them vertically

Time:11-13

I've started learning numpy since yesterday.

my AIM is

Extract odd index elements from numpy array & even index elements from numpy and merge side by side vertically.

Let's say I have the array

mat = np.array([[1, 1, 0, 0, 0],
                [0, 1, 0, 0, 1],
                [1, 0, 0, 1, 1],
                [0, 0, 0, 0, 0],
                [1, 0, 1, 0, 1]])

see this picture

What I've tried.

-->I've done transposing as I've to merge side by by side vertically.

mat = np.transpose(mat)

Which gives me

[[1 0 1 0 1]
 [1 1 0 0 0]
 [0 0 0 0 1]
 [0 0 1 0 0]
 [0 1 1 0 1]]

I've tried accessing odd index elements

odd = mat[1::2] print(odd)

Gives me

[[1 1 0 0 0] ----> wrong...should be [0,1,0,0,1] right? I'm confused

[0 0 1 0 0]] --->wrong...Should be [0,0,0,0,0] right? Where these are coming from?

My final output should like like

[[0 0 1 1 1]
 [1 0 1 0 0]
 [0 0 0 0 1]
 [0 0 0 1 0]
 [1 0 0 1 1]]

Type - np.nd array

CodePudding user response:

Looks like you want:

mat[np.r_[1:mat.shape[0]:2,:mat.shape[0]:2]].T

Output:

array([[0, 0, 1, 1, 1],
       [1, 0, 1, 0, 0],
       [0, 0, 0, 0, 1],
       [0, 0, 0, 1, 0],
       [1, 0, 0, 1, 1]])

Intermediate:

np.r_[1:mat.shape[0]:2,:mat.shape[0]:2]

output: array([1, 3, 0, 2, 4])

CodePudding user response:

While the selection of rows is straight forward, there are various ways of combining them.

In [244]: mat = np.array([[1, 1, 0, 0, 0],
     ...:                 [0, 1, 0, 0, 1],
     ...:                 [1, 0, 0, 1, 1],
     ...:                 [0, 0, 0, 0, 0],
     ...:                 [1, 0, 1, 0, 1]])

The odd rows:

In [245]: mat[1::2,:]     # or mat[1::2]
Out[245]: 
array([[0, 1, 0, 0, 1],
       [0, 0, 0, 0, 0]])

The even rows:

In [246]: mat[0::2,:]
Out[246]: 
array([[1, 1, 0, 0, 0],
       [1, 0, 0, 1, 1],
       [1, 0, 1, 0, 1]])

Joining the rows verticallly (np.vstack can also be used):

In [247]: np.concatenate((mat[1::2,:], mat[0::2,:]), axis=0)
Out[247]: 
array([[0, 1, 0, 0, 1],
       [0, 0, 0, 0, 0],
       [1, 1, 0, 0, 0],
       [1, 0, 0, 1, 1],
       [1, 0, 1, 0, 1]])

But since you want columns - tranpose:

In [248]: np.concatenate((mat[1::2,:], mat[0::2,:]), axis=0).transpose()
Out[248]: 
array([[0, 0, 1, 1, 1],
       [1, 0, 1, 0, 0],
       [0, 0, 0, 0, 1],
       [0, 0, 0, 1, 0],
       [1, 0, 0, 1, 1]])

We could transpose the selections first:

np.concatenate((mat[1::2,:].T, mat[0::2,:].T), axis=1)

or transpose before indexing (note the change in the ':' slice position):

np.concatenate((mat.T[:,1::2], mat.T[:,0::2]), axis=1)

The r_ in the other answer converts the slices into arrays and concatenates them, to make one row indexing array. That's equally valid.

CodePudding user response:

So here alternate is the logic you can use.

 1. convert array to list
 2. Access nested list items based on mat[1::2] - odd & mat[::2] for even
 3. concat them using np.concat at `axis =0` vertically.
 4. Transpose them.

Implementaion.

mat = np.array([[1, 1, 0, 0, 0],
                [0, 1, 0, 0, 1],
                [1, 0, 0, 1, 1],
                [0, 0, 0, 0, 0],
                [1, 0, 1, 0, 1]])
mat_list = mat.tolist() ##############Optional

l_odd = mat_list[1::2]
l_even= mat_list[::2]

mask = np.concatenate((l_odd, l_even), axis=0)
mask = np.transpose(mask)
print(mask)

output #

[[0 0 1 1 1]
 [1 0 1 0 0]
 [0 0 0 0 1]
 [0 0 0 1 0]
 [1 0 0 1 1]]

Checking Type

print(type(mask))

Gives

<class 'numpy.ndarray'>
  • Related