user88383
user88383

Reputation: 197

Fastest method for determining if 2 (vertically or horizontally) adjacent elements of a numpy array have the same value

I am looking for the fastest way of determining if 2 (vertically or horizontally) adjacent elements have the same value.

Let's say I have a numpy array of size 4x4.

array([             
[8, 7, 4, 3],    
[8, 4, 0, 4],          
[3, 2, 2, 1],              
[9, 8, 7, 6]])

I want to be able to identify that there are two adjacent 8s in the first column and there are two adjacent 2s in the third row. I could hard code a check but that would be ugly and I want to know if there is a faster way.

All guidance is appreciated. Thank you.

Upvotes: 3

Views: 1025

Answers (3)

Divakar
Divakar

Reputation: 221554

We would look for differentiation values along rows and columns for zeros signalling repeated ones there. Thus, we could do -

(np.diff(a,axis=0) == 0).any() | (np.diff(a,axis=1) == 0).any()

Or with slicing for performance boost -

(a[1:] == a[:-1]).any() | (a[:,1:] == a[:,:-1]).any()

So, (a[1:] == a[:-1]).any() is the vertical adjacency, whereas the other one is for horizontal one.

Extending to n adjacent ones (of same value) along rows or columns -

from scipy.ndimage.filters import convolve1d as conv

def vert_horz_adj(a, n=1):
    k = np.ones(n,dtype=int)
    v = (conv((a[1:]==a[:-1]).astype(int),k,axis=0,mode='constant')>=n).any()
    h = (conv((a[:,1:]==a[:,:-1]).astype(int),k,axis=1,mode='constant')>=n).any()
    return v | h

Sample run -

In [413]: np.random.seed(0)
     ...: a = np.random.randint(11,99,(10,4))
     ...: a[[2,3,4,6,7,8],0] = 1

In [414]: a
Out[414]: 
array([[55, 58, 75, 78],
       [78, 20, 94, 32],
       [ 1, 98, 81, 23],
       [ 1, 76, 50, 98],
       [ 1, 92, 48, 36],
       [88, 83, 20, 31],
       [ 1, 80, 90, 58],
       [ 1, 93, 60, 40],
       [ 1, 30, 25, 50],
       [43, 76, 20, 68]])

In [415]: vert_horz_adj(a, n=1)
Out[415]: True  # Because of first col

In [416]: vert_horz_adj(a, n=2)
Out[416]: True  # Because of first col

In [417]: vert_horz_adj(a, n=3)
Out[417]: False

In [418]: a[-1] = 10

In [419]: vert_horz_adj(a, n=3)
Out[419]: True  # Because of last row

Upvotes: 4

B. M.
B. M.

Reputation: 18628

if you want to locate the first occurence of each pair :

A=array([             
[8, 7, 4, 3],    
[8, 4, 0, 4],          
[3, 2, 2, 1],              
[9, 8, 7, 6]])

x=(A[1:]==A[:-1]).nonzero()
y=(A[:,1:]==A[:,:-1]).nonzero()

In [45]: x
Out[45]: (array([0], dtype=int64), array([0], dtype=int64))


In [47]: y
Out[47]: (array([2], dtype=int64), array([1], dtype=int64))

In [48]: A[x]
Out[48]: array([8])

In [49]: A[y]
Out[49]: array([2])

x and y give respectively the locations of the first 8 and the first 2.

Upvotes: 0

David Dale
David Dale

Reputation: 11424

You can find the coordinates of the pairs with the following code:

import numpy as np
a = np.array([             
[8, 7, 4, 3],    
[8, 4, 0, 4],          
[3, 2, 2, 1],              
[9, 8, 7, 6]])

vertical = np.where((a == np.roll(a, 1, 0))[1:-1])
print(vertical) # (0,0)  is the coordinate of the first of the repeating 8's
horizontal = np.where((a == np.roll(a, 1, 1))[:, 1:-1])
print(horizontal) # (2,1)  is the coordinate of the first of the repeating 2's 

which returns

(array([0], dtype=int64), array([0], dtype=int64))
(array([2], dtype=int64), array([1], dtype=int64))

Upvotes: 1

Related Questions