Finding equal rows in a matrix

18 Ansichten (letzte 30 Tage)
Ozge Moral
Ozge Moral am 7 Nov. 2015
Bearbeitet: Ozge Moral am 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.

Akzeptierte Antwort

Image Analyst
Image Analyst am 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 Kommentar
Ozge Moral
Ozge Moral am 7 Nov. 2015
Bearbeitet: Ozge Moral am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by