MATLAB: Pairs of consecutive numbers

arraymatrix

Hello,
I have a vector A=[1 2 5] i would like to have something that stores the pair of consecutive numbers eg B= [1 2] and something that stores the values unused eg [C= 5]
i would like this working for A=[5 2 1], A=[5 1 2] and all the permutations of the elements .
Thanks
Here is some piece of code that I have tried but I have no idea how to get the element unused, 'C' in my example above
A= [6 3 4];
p=[find(diff(A)==1); find(diff(A) == -1)];
q=[p;p+1];
A(q)

Best Answer

Well, you're almost there. When the diff is 1 or -1, this means that the number at the same index and the next one are consecutive. The easiest way to find the non-consecutive numbers is to take the set difference of all the indices with the pair indices.
A = [6 2 3 2 4 1 3 2 1 0 5]
pairindices = find(ismember(diff(A), [-1, 1])) + [0;1] %assumes A is a row vector
nonconsindices = setdiff(1:numel(A), pairindices)
Another method, which uses the undocumented feature that strfind works with numeric vectors:
nonconsindices = strfind([0, abs(diff(A)) == 1, 0], [0, 0])