I would like to do something similar to the following, except I would only like to remove ‘g’ and’g’ because they are the duplicates that occur one after each other. I would also like to keep the sequence the same.
Any help would be appreciated!!!
I have this cell array in MATLAB:
y = { 'd' 'f' 'a' 'g' 'g' 'w' 'a' 'h'}
ans =
'd' 'f' 'a' 'w' 'a' 'h'
There was an error in my first answer (below) when used on multiple duplicates (thanks grantnz). Here’s an updated version:
>> y = { 'd' 'f' 'a' 'g' 'g' 'w' 'a' 'h' 'h' 'i' 'i' 'j'}; >> i = find(diff(char(y)) == 0); >> y([i; i+1]) = [] y = 'd' 'f' 'a' 'w' 'a' 'j'OLD ANSWER
If your “cell vector” always contains only single character elements you can do the following:
>> y = { 'd' 'f' 'a' 'g' 'g' 'w' 'a' 'h'} y = 'd' 'f' 'a' 'g' 'g' 'w' 'a' 'h' >> y(find(diff(char(y)) == 0) + [0 1]) = [] y = 'd' 'f' 'a' 'w' 'a' 'h'