MATLAB: Fastest Way to Count Unique # of 2 Number Combinations in a Vector of Integers In case anyone is interested, the application that I have in mind fo this is as follows: X is basically a sample path in a Markov Chain and COUNTS is a basic way to create the transition matrix so that after normalization, COUNTS(i,j) = P(next state = j | current state = i).