Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locating duplicate numbers in cells

Tags:

matlab

I have a cell containing a number of arrays of numbers.

I need to find the duplicates (if any), and remove the shortest array, containing any duplicates.

Example: In c = {[1 2 3] [4 5 6] [1 7 8 9]} the number one is duplicate, and hence the cell should be c = {[4 5 6] [1 7 8 9]}, since [1 2 3] is the shortest array.

The size of the cell and the arrays varies.

like image 374
Allan Nørgaard Avatar asked Dec 03 '25 16:12

Allan Nørgaard


1 Answers

This can be done using the union function, which does a set wise union on 2 vectors:

 c = {[1 2 3] [4 5 6] [1 7 8 9]}
 remove=[];
 for k=1:length(c)
     for l=k+1:length(c)
         if length(union(c{k},c{l}))<length(c{k})+length(c{l}) 
             if length(c{k})<=length(c{l})
                  remove=[remove;k];
             else
                  remove=[remove;l];
             end
         end
     end
 end
 for k=1:length(remove)
     c(remove)=[];
 end
like image 160
jpjacobs Avatar answered Dec 06 '25 08:12

jpjacobs