For an nx3 matrix, order the 3 entries in each row, then order rows from least to greatest, then check for repeated rows.

7 次查看(过去 30 天)
I have a matrix that is nx3, and am trying to do the following with it: 1) For each row, sort its three entries from smallest to largest. 2) Order the rows from smallest to greatest by the first entry (using the sortrows function). 3) Check for repeated rows in the matrix. If the row of three numbers appears more than once, I would like to delete all of its appearances from the matrix. In the end, the output should be a matrix containing all rows that only appeared once in the original.
Any help would be appreciated with this. Thank you!

回答(2 个)

Nirmal
Nirmal 2012-6-20
This should do the job for you.
a=rand(5,3);
[m,n]=size(a);
a=sort(a,2);
a=sortrows(a,3);
temp=[];
for i=1:m
flag=0;
for j=1:m
if i==j
continue;
end
if sum(a(i,:)==a(j,:))==3
flag=1;
break;
end
end
if flag==0
temp=[temp;a(i,:)];
end
end
a=temp;

Sean de Wolski
Sean de Wolski 2012-6-20
In R2012a or later, this will do it:
X = [[1 3 2;1 3 2]; rand(10,3)];
[Y,~,idx] = unique(sortrows(sort(X,2,'ascend'),1:3),'rows','stable');
idx2keep= accumarray(idx,1)<=1;
Y = Y(idx2keep,:);

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by