Reputation: 659
I have a string: pairs = ['AA';'AB';'BB';'AC'; 'CC'; 'CB';'DE'; 'BC']
How can I delete the element which has the same characters in its string like 'AA','BB', 'CC' ?
The expected output should be: out = ['AB';'AC';'CB';'DE';'BC']
Upvotes: 0
Views: 373
Reputation: 13945
If all the entries are only 2 elements, you could subtract each elements and if the result is 0 then both elements are the same.
Example:
pairs = {'AA';'AB';'BB';'AC'; 'CC'; 'CB';'DE'; 'BC'}
Diffs = cellfun(@(x) diff(x),pairs)
Diffs
looks like this:
Diffs =
0
1
0
2
0
-1
1
1
Now delete those entries:
pairs(~Diffs) = []
pairs =
'AB'
'AC'
'CB'
'DE'
'BC'
Upvotes: 1
Reputation: 9864
Use logical indexing and compare first and second column:
out = pairs(pairs(:,1)~=pairs(:,2),:)
For a more general way (to cover rows with more than two characters) you can create the index of rows that have all elements equal to each other using bsxfun:
allsame = any(~bsxfun(@eq, pairs, pairs(:,1)), 2);
out = pairs(allsame,:);
Upvotes: 3