Finding equal rows in a matrix

17 visualizzazioni (ultimi 30 giorni)
Ozge Moral
Ozge Moral il 7 Nov 2015
Modificato: Ozge Moral il 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.

Risposta accettata

Image Analyst
Image Analyst il 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 Commento
Ozge Moral
Ozge Moral il 7 Nov 2015
Modificato: Ozge Moral il 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Resizing and Reshaping Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by