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!
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!