unique函数在使用时会自动升序排序,但是在实际应用中,我们并不希望改变原有矩阵的顺序,只是希望去除重复行。这段代码可以实现去除重复行而不对原有矩阵进行排序。
a=[8 9 7;1 5 2;9 4 6;8 2 9;8 9 4;1 5 2]; [aa,indexaa]=unique(a,'rows','first'); [indresult,newindex]=sort(indexaa); for i=1:length(indexaa) C(i,:)=aa(newindex(i),:); end