2012-03-05 35 views
3

我有一個包含許多數字數組的單元格。在單元格中查找重複的號碼

我需要找到重複項(如果有的話),並刪除最短的數組,包含任何重複項。

示例:在c = {[1 2 3] [4 5 6] [1 7 8 9]}中,號碼1重複,因此該單元格應該是c = {[4 5 6] [1 7 8 9]},因爲[1 2 3]是最短的數組。

單元格大小和數組大小不同。

回答

1

這可以通過使用union功能,做一個聰明的設定工會上做2個載體:

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 
+0

的伎倆。不知道工會的功能,謝謝。 – 2012-03-05 15:19:46