# MATLAB: Removing repeated numbers in a matrix

remove duplicates matrix

Ex: In a matrix
``A = [9 9 1 1 2 2 2 2 2 0 3 3; 7 7 4 4 4 5 5 6 6 6 6 6 ; 8 8 7 7 7 8 8 8 9 9 9 5]``
, i want to eliminate all repeating elements and retain non-repeating elements
i.e. from the above example
``A = [9 1 2 0 3 ; 7 4 5 6 ; 8 7 8 9 5]``
To prevent a dimensions error because of different output results, the remaining entries on the left can be filled by zero like here
``A = [9 1 2 0 3 ; 7 4 5 6 0 ; 8 7 8 9 5]``

• ``>> A = [9,9,1,1,2,2,2,2,2,0,3,3;7,7,4,4,4,5,5,6,6,6,6,6;8,8,7,7,7,8,8,8,9,9,9,5]A =   9   9   1   1   2   2   2   2   2   0   3   3   7   7   4   4   4   5   5   6   6   6   6   6   8   8   7   7   7   8   8   8   9   9   9   5>> X = diff(A(:,[1,1:end]),1,2)~=0;>> X(:,1) = true;>> S = size(A);>> [R,~] = ndgrid(1:S(1),1:S(2));>> C = cumsum(+X,2);``
``>> B = accumarray([R(:),C(:)],A(:),[],@mode,0)B =   9   1   2   0   3   7   4   5   6   0   8   7   8   9   5``
``>> B = zeros(S(1),max(C(:)));>> B(sub2ind(S,R,C)) = AB =   9   1   2   0   3   7   4   5   6   0   8   7   8   9   5``