0

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']

2 Answers 2

3

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,:);
Sign up to request clarification or add additional context in comments.

Comments

1

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'

1 Comment

Note though that the question had pairs as an array of strings. Instead of defining a new cell array as you have done, you could simply convert from the string array to a cell array, as pairs = cellstr(pairs);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.