hgaarous
hgaarous

Reputation: 51

Eliminate/Remove duplicates from array Matlab

How can I remove any number that has duplicate from an array.

for example:

b =[ 1 1 2 3 3 5 6]

becomes

b =[ 2 5 6]

Upvotes: 0

Views: 4095

Answers (2)

m7913d
m7913d

Reputation: 11064

You can first sort your elements and afterwards remove all elements which have the same value as one of its neighbors as follows:

A_sorted = sort(A); % sort elements
A_diff = diff(A_sorted)~=0; % check if element is the different from the next one 
A_unique = [A_diff true] & [true A_diff]; % check if element is different from previous and next one
A = A_sorted(A_unique); % obtain the unique elements.

Benchmark

I will benchmark my solution with the other provided solutions, i.e.:

  • using diff (my solution)
  • using hist (rahnema1)
  • using sum (Jean Logeart)
  • using unique (my alternative solution)

I will use two cases:

  • small problem (yours): A = [1 1 2 3 3 5 6];
  • larger problem

    rng('default');
    A= round(rand(1, 1000) * 300);
    

Result:

                  Small        Large       Comments
----------------|------------|------------%----------------
 using `diff`   | 6.4080e-06 | 6.2228e-05 % Fastest method for large problems
 using `unique` | 6.1228e-05 | 2.1923e-04 % Good performance
 using `sum`    | 5.4352e-06 | 0.0020     % Only fast for small problems, preserves the original order
 using `hist`   | 8.4408e-05 | 1.5691e-04 % Good performance

My solution (using diff) is the fastest method for somewhat larger problems. The solution of Jean Logeart using sum is faster for small problems, but the slowest method for larger problems, while mine is almost equally fast for the small problem.

Conclusion: In general, my proposed solution using diff is the fastest method.


timeit(@() usingDiff(A))
timeit(@() usingUnique(A))
timeit(@() usingSum(A))
timeit(@() usingHist(A))

function A = usingDiff (A)
    A_sorted = sort(A);
    A_unique = [diff(A_sorted)~=0 true] & [true diff(A_sorted)~=0];
    A = A_sorted(A_unique);
end

function A = usingUnique (A)
    [~, ia1] = unique(A, 'first');
    [~, ia2] = unique(A, 'last');
    A = A(ia1(ia1 == ia2));
end

function A = usingSum (A)
    A = A(sum(A==A') == 1);
end

function A = usingHist (A)
    u = unique(A);
    A = u(hist(A, u) ==1);
end

Upvotes: 0

rahnema1
rahnema1

Reputation: 15837

Use unique function to extract unique values then compute histogram of data for unique values and preserve those that have counts of 1.

a =[ 1 1 2 3 3 5 6];
u = unique(a)
idx = hist(a, u) ==1;
b = u(idx)

result

  2 5 6

for multi column input this can be done:

a = [1 2; 1 2;1 3;2 1; 1 3; 3 5 ; 3 6; 5 9; 6 10] ;
[u ,~, uid] = unique(a,'rows');
idx = hist(uid,1:size(u,1))==1;
b= u(idx,:)

Upvotes: 1

Related Questions