Here's a simple approach using base R (single thread). Let's start from a boolean matrix represented as 0,1:
mat <- as.integer(rnorm(10*10) > 0) |>
matrix(nrow = 10)
##> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##> [1,] 0 1 0 0 1 1 1 1 0 0
##> [2,] 0 0 0 0 0 1 1 0 0 0
##> [3,] 1 1 1 0 1 0 1 1 1 0
##> [4,] 0 1 1 0 1 0 1 1 1 0
##> [5,] 0 1 0 0 0 0 0 1 0 1
##> [6,] 1 0 0 0 0 0 1 1 1 0
##> [7,] 0 0 0 0 1 1 1 1 0 0
##> [8,] 0 1 0 1 1 1 1 0 1 1
##> [9,] 1 0 0 1 0 1 1 1 1 1
##>[10,] 0 1 1 0 1 0 0 0 0 1
The value S[i,j] representing sum(mat[i,] & mat[j,]) is also given by the scalar product of mat[i,] and mat[j,]. Therefore, the matrix S can be obtained by the matrix product of mat and t(mat):
S <- mat %*% t(mat)
The sum C[i] of true values in each row mat[i,] can be calculated straightforwardly:
C <- apply(mat, 1, sum)
Then, we obtain the matrix H where the element H[i,j] is the minimum of C[i] and C[j].
H <- outer(C, C, "pmin")
Finally, we divide S by H to obtain the desired matrix:
S/H
##> [,1] [,2] [,3] [,4] [,5] [,6] [,7]
##> [1,] 1.0000000 1.0 0.8000000 0.8000000 0.6666667 0.5000000 1.0000000
##> [2,] 1.0000000 1.0 0.5000000 0.5000000 0.0000000 0.5000000 1.0000000
##> [3,] 0.8000000 0.5 1.0000000 1.0000000 0.6666667 1.0000000 0.7500000
##> [4,] 0.8000000 0.5 1.0000000 1.0000000 0.6666667 0.7500000 0.7500000
##> [5,] 0.6666667 0.0 0.6666667 0.6666667 1.0000000 0.3333333 0.3333333
##> [6,] 0.5000000 0.5 1.0000000 0.7500000 0.3333333 1.0000000 0.5000000
##> [7,] 1.0000000 1.0 0.7500000 0.7500000 0.3333333 0.5000000 1.0000000
##> [8,] 0.8000000 1.0 0.5714286 0.6666667 0.6666667 0.5000000 0.7500000
##> [9,] 0.6000000 1.0 0.5714286 0.5000000 0.6666667 1.0000000 0.7500000
##>[10,] 0.5000000 0.0 0.7500000 0.7500000 0.6666667 0.0000000 0.2500000
##> [,8] [,9] [,10]
##> [1,] 0.8000000 0.6000000 0.5000000
##> [2,] 1.0000000 1.0000000 0.0000000
##> [3,] 0.5714286 0.5714286 0.7500000
##> [4,] 0.6666667 0.5000000 0.7500000
##> [5,] 0.6666667 0.6666667 0.6666667
##> [6,] 0.5000000 1.0000000 0.0000000
##> [7,] 0.7500000 0.7500000 0.2500000
##> [8,] 1.0000000 0.7142857 0.7500000
##> [9,] 0.7142857 1.0000000 0.2500000
##>[10,] 0.7500000 0.2500000 1.0000000