Finding equal rows in a matrix
16 次查看(过去 30 天)
显示 更早的评论
I have a problem with finding equal elements in array. I searched and read all post about it, but i couldn't solve my problem. I have a 20x2 double array, t=0 and n=20. I want to find equal elements and result their row number. But i didn't. My code is at below :
for i=1:n
for j=2:n
if neigbor(i,:,t)==neigbor(j,:,t)
g(i,:)=i;
else
g(i,:)=0;
end
end
end
I also tried unique and ismember functions. Like this:
[x,y,z] = unique(neigbor,'rows');
Not further. My neigbor(i,:,t) is at below, in this sample i want to see 8,9 and 10 row numbers.
neigbor =
-4.2500 -3.5000
-3.5000 -2.7500
-2.7500 -2.0000
-2.0000 -1.2500
-1.2500 -0.5000
-0.5000 0.2500
0.2500 1.0000
1.0000 1.7500
1.0000 1.7500
1.0000 1.7500
1.7500 2.5000
2.5000 3.2500
3.2500 4.0000
4.0000 4.7500
4.7500 5.5000
5.5000 6.2500
6.2500 7.0000
7.0000 7.7500
7.7500 8.5000
8.5000 9.2500
Where is my mistake? Can you make a suggestion or give any hint? If I repeat this question, sorry for that.
0 个评论
采纳的回答
Image Analyst
2015-11-7
This works. It could be made more compact but I wanted you to see each step of the way.
neigbor =[...
-4.2500 -3.5000
-3.5000 -2.7500
-2.7500 -2.0000
-2.0000 -1.2500
-1.2500 -0.5000
-0.5000 0.2500
0.2500 1.0000
1.0000 1.7500
1.0000 1.7500
1.0000 1.7500
1.7500 2.5000
2.5000 3.2500
3.2500 4.0000
4.0000 4.7500
4.7500 5.5000
5.5000 6.2500
6.2500 7.0000
7.0000 7.7500
7.7500 8.5000
8.5000 9.2500]
differencesFromRowAbove = diff(neigbor)
allZeroRows = all(differencesFromRowAbove == 0, 2)
rowsToKeep = find(allZeroRows)
% Add on first one
rowsToKeep = [rowsToKeep(1); rowsToKeep+1]
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!