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

11 visualizzazioni (ultimi 30 giorni)
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!

Risposte (2)

Nirmal
Nirmal il 20 Giu 2012
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 il 20 Giu 2012
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,:);

Categorie

Scopri di più su Shifting and Sorting Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by