Search code examples
stringmatlabcellcell-array

Matlab, order cells of strings according to the first one


I have 2 cell of strings and I would like to order them according to the first one.

A = {'a';'b';'c'}
B = {'b';'a';'c'}

idx = [2,1,3] % TO FIND

B=B(idx);

I would like to find a way to find idx...


Solution

  • This seems to be significantly faster than using ismember (although admittedly less clear than @rayryeng's answer). With thanks to @Divakar for his correction on this answer.

    [~, indA] = sort(A);
    [~, indB] = sort(B);
    idx = indA(indB);