In the matched analysis of an observational study, confounding on covariates X is addressed by comparing members of a distinguished group (Z = 1) to controls (Z = 0) only when they belong to the same matched set. The better matchings, therefore, are those whose matched sets exhibit both dispersion in Z and uniformity in X. For dispersion in Z, pair matching is best, creating matched sets that are equally balanced between the groups; but actual data place limits, often severe limits, on matched pairs' uniformity in X. At the other extreme is full matching, the matched sets of which are as uniform in X as can be, while often so poorly dispersed in Z as to sacrifice efficiency. This article presents an algorithm for exploring the intermediate territory. Given requirements on matched sets' uniformity in X and dispersion in Z, the algorithm first decides the requirements' feasibility. In feasible cases, it furnishes a match that is optimal for X-uniformity among matches with Z-dispersion as stipulated. To illustrate, we describe the algorithm's use in a study comparing womens' to mens' working conditions; and we compare our method to a commonly used alternative, greedy matching, which is neither optimal nor as flexible but is algorithmically much simpler. The comparison finds meaningful advantages, in terms of both bias and efficiency, for our more studied approach.