This is not the fastest solution (both Sergio's and Daniel's are faster).

My approach is to handle the measurements one by one.

I assume that measurements are few relative to number of rows.

So my suggestion becomes:

it is 22% faster than #1, but with fewer measurements it becomes faster.

What you get is a full, but symmetric matrix similar to the requested answer.

My approach is to handle the measurements one by one.

I assume that measurements are few relative to number of rows.

So my suggestion becomes:

Code:

mata: R = rows(X) C = cols(X) A2 = J(N, N, 0) timer_clear(1) timer_on(1) for (reps = 1; reps <= `nreps' ; reps++) { // outer loop just for timing for (c = 1; c <= C; c++) A2 = A2 + (J(R, 1, X[,c]') :== X[,c]) } timer_off(1) timer() end

What you get is a full, but symmetric matrix similar to the requested answer.

## Comment