在MATLAB中删除具有相同值的所有行

时间:2012-12-14 17:25:54

标签: matlab matlab-deployment delete-row

我有一个像这样的矩阵:

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

我希望删除相同的行。 我的新矩阵应该是

4 5 6
7 9 6

我该怎么做?

4 个答案:

答案 0 :(得分:5)

基于@tayler的unique( ,'rows')的更完整的解决方案将是

[uA, ~, ui] = unique(A, 'rows'); % we have a single copy of each row.
% it is now left to determine which row is duplicate 
n = hist( ui, 1:max(ui) );
sel = n == 1; % pick only indices that appear once
uA = uA(sel, :);

答案 1 :(得分:4)

开始的好地方是:

b = unique(A, 'rows')

答案 2 :(得分:1)

您可以尝试从所有其他行中减去一行,如果任何行包含所有零,您知道它包含重复元素。

答案 3 :(得分:1)

我认为这个脚本可能会做你想要的:

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)