Eliminate/Remove duplicates from array Matlab

前端 未结 2 964
慢半拍i
慢半拍i 2020-12-12 06:42

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]
         


        
2条回答
  •  执念已碎
    2020-12-12 06:57

    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,:)
    

提交回复
热议问题