How to convert a discrete array to continuous array using numpy?

Viewed 56

I am having an array a = np.array([3,5,7,8,10]). I want to convert this into b = ([0,0,1,0,1,0,1,1,0,1]). Array b is basically pointing to the position in the array a (converting it to 0 and 1). How can i do that ?. Intention is to use np.convolve to identify where there is a window of continuous numbers. I have tried to use b = np.digitize(c,a), where c = np.arange(1,10,1). But it gives error both a, b are not in same size.

1 Answers

You can try something like

a.sort()
b = [1 if i in a else 0 for i in range(1,a[-1]+1)]

The output is [0, 0, 1, 0, 1, 0, 1, 1, 0, 1]

You can skip a.sort() if the array is already sorted.

Related