For an nx3 matrix, order the 3 entries in each row, then order rows from least to greatest, then check for repeated rows.
5 次查看(过去 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!
0 个评论
回答(2 个)
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;
0 个评论
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,:);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!