Create a binary adjacency matrix from a vector of indices

Viewed 733

Suppose I have a vector that looks like this:

x <- sample(5, 500, replace = TRUE)

so that each element corresponds to some index from 1 through 5.

What's an efficient way to create a binary adjacency matrix from this vector? To elaborate, the matrix A should be such that A[i,j] = 1 if x[i] = x[j] and 0 otherwise.

1 Answers
Related