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

1 回表示 (過去 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 Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by