Finding equal rows in a matrix

17 vues (au cours des 30 derniers jours)
Ozge Moral
Ozge Moral le 7 Nov 2015
Modifié(e) : Ozge Moral le 7 Nov 2015
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.

Réponse acceptée

Image Analyst
Image Analyst le 7 Nov 2015
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 commentaire
Ozge Moral
Ozge Moral le 7 Nov 2015
Modifié(e) : Ozge Moral le 7 Nov 2015
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by