1

The following code removes duplicate values

numarr = [1 2 2 3 3 3 4 4 4 4];
%// Filter doubled values
[n_, bin]       = histc(numarr, unique(numarr));
multiple_       = find(n_ > 1);
mult_indices_   = ismember(bin, multiple_);

numarr(mult_indices_) = [];

%// output     
numarr =  1

How to adapt it, that the first occurence of any duplicate remains?

i.e. that the output will be

numarr =

     1     2     3     4
1
  • 2
    Try matlab's unique function. Commented Feb 5, 2015 at 18:32

1 Answer 1

2

Use unique with the 'stable' property:

a = [1 3 2 5 2 7 1 3 4 5 6 8 2];
output = unique(a,'stable')

which will keep the order and therefore keeps the first occurrance of every value, as desired.

output =

     1     3     2     5     7     4     6     8

Without 'stable' the output gets sorted.


Regarding your comment: To get the indices of the removed duplicates you need the second output of unique and setdiff:

[output, kept_idx] = unique(a,'stable')
removed_idx = setdiff(1:numel(a),kept_idx)

removed_idx =

     5     7     8    10    13
Sign up to request clarification or add additional context in comments.

3 Comments

Nice..what I forgot to mention...is it possible to retrieve the indices of the removed duplicates (i. e. the indices of their locations before the have been removed)?
This I would not have figured out myself
@embert that's what we are here for ;)

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.