Computing the spectral gap of a family of matrices