MATLAB: How to create a random permutation from two sets of numbers in one vector

random permutationtwo sets

Assume that I have two vectors:
a = 1 2 3 4 5
b= 6 7 8 9 10
how can I generate a vector of random permutation of combining a and b, in this case each of a and b contain at least one element and a should be always appear before b. Also elements of a and b can appear individual but not combined. Here is an example (I just used dashes between numbers to make it clear but c is a row vector):
c = a b – a b – a b – a – b
equals to:
c = 1 2 6 – 3 7 8 – 4 9 – 5 – 10
I also need to compute the number of sets (between dashes), i.e in this case c consist of 5 sets as between dashes.
Note that the sequence can be different in each time, so c can be as
c = a – a b – b – a b – b ….. and so on.
Sorry for the confusing explanation.

Best Answer

Separation in alternate groups
c = [1 2 6 3 7 8 4 9 5 10];
from_a = c <= 5; % change accordingly, logical array, TRUE if element is from a
splitc = mat2cell(c,1,diff(find([true,diff(from_a),true])));
isa = xor(~from_a(1),mod(1:length(splitc),2));
splitc = splitc(:);
isa = isa(:);
T = table(splitc,isa)