0

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
siegel
  • 819
  • 2
  • 12
  • 24

2 Answers2

3

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
Oleg
  • 10,406
  • 3
  • 29
  • 57
2

This will do:

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

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • I accidentally oversimplified my question a bit: what if `CELLS = {'AB','AB','AB','BC','BC','CD','CD','CD','DF','FG'}`? – siegel Aug 02 '13 at 18:27