I have an array y, len(y) = M
that contains values from 0 -> N
. For example, with N = 3
:
y = [0, 2, 0, 1, 2, 1, 0, 2]
Incidence matrix A
is defined as followed:
- Size
MxM
A(i,j) = 1 if y(i) == y(j)
A(i,j) = 0 if y(i) != y(j)
A simple algorithm would be:
def incidence(y):
M = len(y)
A = np.zeros((M,M))
for i in range(M):
for j in range(M):
if y[i]==y[j]:
A[i,j] = 1
return A
But this is very slow. Is there any way to do this faster? Using list comprehension or vectorization, for example.