MATLAB: How to replace repeating no.s in an array by distinct no.

repeating no. sto replace

Let n=10 Consider array A={7,6,3,5,3,6,6,2,9,6} Algorithm: replace the repeating no. by its maximum unused value & I want all no.s between 1 & 10. How can it be done??

Best Answer

  • A=[7,6,3,5,3,6,6,2,9,6]
    [a,b] = unique(A,'first');
    out = zeros(10,1);
    out(b) = a;
    out(out==0) = setdiff(1:10,a)