siegel
siegel

Reputation: 829

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

Upvotes: 0

Views: 169

Answers (2)

Oleg
Oleg

Reputation: 10676

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

Upvotes: 3

Luis Mendo
Luis Mendo

Reputation: 112749

This will do:

CHANGES = find(diff(cell2mat(CELLS)))+1

Upvotes: 2

Related Questions