Search code examples
matlabcell-array

Matlab: finding change points in cell array


If I have a cell array

CELLS = {'AB','AB','AB','BC','BC','CD','CD','CD','DF','FG'}

How do I find the indices of the locations at which the elements change?

in this example I'm looking for an output like:

CHANGES = 
        4 
        6 
        9
        10

Solution

  • For a generic cell array of string call unique(), and find(diff(...)) the position index:

    s = {'AB','AB','AB','BC','BC','CD','CD','CD','DF','FG'};
    [~,~,p] = unique(s)
    find(diff(p)==1)+1