Finding equal rows in a matrix

조회 수: 15 (최근 30일)
Ozge Moral
Ozge Moral 2015년 11월 7일
편집: Ozge Moral 2015년 11월 7일
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.

채택된 답변

Image Analyst
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]
  댓글 수: 1
Ozge Moral
Ozge Moral 2015년 11월 7일
편집: Ozge Moral 2015년 11월 7일
Understood what i was missing. It can be written as shortly,
z = find(all(diff(neigbor)==0,2)
Thank you a lot for writing step by step, it is useful.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by