Reputation: 659
Input= 'DA EA DA BD FA ED GE AA CA BB CC FB BC CB CF'
How can I delete repeated pairs in the above input and delete the pairs which have the same characters as well ? In this case, the pairs should be deleted are : the second 'DA', 'AA', 'BB', 'CC'.
The expected output should be:
Output= 'DA EA BD FA ED GE CA FB BC CB CF'
Upvotes: 1
Views: 68
Reputation: 8566
this works for me.
Input= 'DA EA DA BD FA ED GE AA CA BB CC FB BC CB CF'
foo = reshape([Input,' '], 3, 15)'
foo(foo(:,1)==foo(:,2),:)=''
bar = unique(foo,'rows','stable')
reshape(bar', 1, numel(bar))
Upvotes: 1
Reputation: 104503
Use strsplit
to split up the strings using the space character as a delimiter, then use unique
with the 'stable'
flag to ensure we remove duplicates and ensuring that the unique strings are in the same order. This removes duplicate strings, but not strings that all contain the same character. To do that, we'll loop through each string and check to see if the difference between successive characters is all equal to 0. We use diff
over each string in combination with all
to do this for us. If all of the successive differences are equal to 0, we filter these out from the split strings. After, we use strjoin
to join all of the strings back:
%// Input string
s = 'DA EA DA BD FA ED GE AA CA BB CC FB BC CB CF';
%// Split string up into cells based on spaces
st = strsplit(s, ' ');
%// Filter out duplicate strings
st = unique(st, 'stable');
%// Find split strings that all have the same characters
f = cellfun(@(x) all(diff(x) == 0), st);
%// Remove from the list
st2 = st(~f);
%// Join the remaining strings back
sf = strjoin(st2, ' ');
sf
is the final output. In this case, we get:
sf =
DA EA BD FA ED GE CA FB BC CB CF
The good thing about the above approach is that the strings in between the spaces can be any size - it doesn't have to be just two characters per string.
Upvotes: 2