Search code examples
matlabmatlab-deploymentdelete-row

Remove all the rows with same values in MATLAB


I've a matrix like this:

1 2 4
4 5 6
1 2 4
7 9 6
1 2 4

and i want delete the same rows. My new matrix should be

4 5 6
7 9 6

how can I do this?


Solution

  • I think this script may do what you want:

    B= A;
    position = 1;
    condition = true;
    bSize = size(B,1);
    while (position < bSize)
        [~,~,ic] = unique(B,'rows');
        changes = find(ic(position:end,:)== ic(position));
        if (length(changes)>1)
            B(changes+position-1,:)= [];
            bSize = size(B,1);
        else
            position = position+1;
        end
    end
    disp(B)