MATLAB: How to sort one array based on another of a different size

cell arraysMATLABsort

Hello,
I would like to sort one cell array A according to the order define by a separate cell array B of a smaller size. For example:
A = [a a b c c d d d d e e]
B = [d a c b e]
After sorting, array A would be:
A = [d d d d a a c c b e e]
Thank you for the help.

Best Answer

As Rik wrote, the MATLAB solution is to use ismember, e.g.:
>> A = {'a','a','b','c','c','d','d','d','d','e','e'};
>> B = {'d','a','c','b','e'};
>> [X,Y] = ismember(A,B);
>> [~,Z] = sort(Y(X));
>> C = A(Z)
C =
'd' 'd' 'd' 'd' 'a' 'a' 'c' 'c' 'b' 'e' 'e'