How to find similar rows in two matrices?

I have two long matrices with 3 rows, namly the radii, the x- and the y-coordinates of a circle. Now I would like to compare the two matrices in order to find out which ones belong to each other (meaning: which ones have the similar values). Unfortunatly, the matrices do NOT have the same amount of rows (one has 70, the other one has 74). dimension does not fit...
How would you do it? It tried to add rows containing zeros and substrate the matrices. thats not a elegant way to do it, though.
My goal is a third matrix containing only the similar circles....

Answers (1)

You could try ismember with the 'rows' option. But this will require them to be equal not similar. Alternatively, you could write something that implements an are rows close type of thing. We could help you with that if you could provide a dataset for us to work with.
More
Here is how I would approach this. Basically we want to take the sqrt sum of squared differences between each row combination. I would do this using the following:
MyDists = reshape(sqrt(sum(bsxfun(@minus,mB1,reshape(mB2.',1,3,[])).^2,2)),size(mB1,1),size(mB2,1));
Now MyDists will contain the distance between each rows in a matrix format.
If you put a threshold on this you can then determine which rows/columns are within a certain distance of each other.

4 Comments

mB1 =
3.0000 84.2386 292.8571
4.0000 106.6330 609.5905
4.0000 140.5229 801.7737
4.0000 27.0903 236.9938
4.0000 33.7701 987.2504
5.0000 145.1516 342.2777
5.0000 218.6190 452.8426
5.0000 218.9364 124.3950
5.0000 73.6517 658.9759
5.0000 295.2959 496.6025
5.0000 10.4302 649.0322
5.0000 65.4731 912.7583
5.0000 187.6887 654.8640
5.0000 350.7198 160.0053
5.0000 27.5837 323.1575
6.0000 184.0949 927.8066
6.0000 70.6457 803.9617
6.0000 35.5727 686.9287
6.0000 66.5224 412.8655
6.0000 473.2779 667.8386
6.0000 19.9922 548.0229
6.0000 392.8298 546.2898
6.0000 9.4267 564.2696
6.0000 13.6125 858.7830
7.0000 187.0649 600.8996
7.0000 411.9599 914.8573
7.0000 262.2102 770.1153
7.0000 107.4194 407.3231
7.0000 56.2066 637.5961
7.0000 199.3653 466.6608
7.0000 105.9599 779.3439
7.0000 68.1422 556.0996
7.0000 90.2716 50.3280
7.0000 394.2781 105.9204
7.0000 91.2792 422.8783
7.0000 50.4056 800.7158
7.0000 29.0610 484.5180
7.0000 109.4590 842.2688
7.0000 21.4602 223.1551
8.0000 188.2274 102.4243
8.0000 32.8322 467.8996
8.0000 207.9099 505.1574
8.0000 210.5194 130.5524
8.0000 134.3207 28.4294
8.0000 317.8802 848.9086
8.0000 110.8562 102.9950
8.0000 165.3087 365.1935
9.0000 433.9346 363.0698
9.0000 250.1670 630.3742
9.0000 289.3263 833.7752
9.0000 53.9224 524.8930
9.0000 231.3526 810.2561
9.0000 237.9043 740.3904
9.0000 95.5607 80.4492
9.0000 50.5669 770.8235
10.0000 427.4762 968.9869
10.0000 102.2991 337.0033
10.0000 226.8764 268.7791
10.0000 167.1554 801.0665
10.0000 41.7510 919.2462
10.0000 54.4706 598.9854
11.0000 270.1133 68.2401
11.0000 123.2072 675.8103
11.0000 26.2356 668.8749
11.0000 74.2291 456.7574
12.0000 410.3451 531.7527
12.0000 100.6608 741.2848
12.0000 161.1131 502.8072
12.0000 81.8380 569.2056
15.0000 133.6312 401.7920
mB2 =
4.0000 25.3083 632.5705
4.0000 169.1286 313.2152
4.0000 242.6512 537.7448
4.0000 20.3139 173.5782
4.0000 16.1696 772.4642
5.0000 213.2326 75.4309
5.0000 306.8244 428.2689
5.0000 25.5995 677.6045
5.0000 223.9952 413.4619
5.0000 50.3906 793.8978
5.0000 127.7463 35.8106
6.0000 444.0849 316.5527
6.0000 108.0634 270.9730
6.0000 248.7814 624.0894
6.0000 225.3186 60.4929
6.0000 83.6197 510.9573
6.0000 230.0475 834.9254
6.0000 473.1621 661.6225
6.0000 258.6605 770.4566
6.0000 17.8455 691.3873
6.0000 71.2870 351.4614
6.0000 159.9689 623.4049
7.0000 408.8205 915.8435
7.0000 190.2172 559.2566
7.0000 110.2720 349.0485
7.0000 60.7199 370.5793
7.0000 193.0445 465.6853
7.0000 57.1913 478.6240
7.0000 114.1407 780.7945
7.0000 417.7772 445.9167
7.0000 61.3892 158.3085
8.0000 106.5314 689.8125
8.0000 82.5007 261.5752
8.0000 104.6850 590.0081
8.0000 125.3916 648.9729
8.0000 141.7725 776.9893
8.0000 40.8027 860.6428
8.0000 41.3871 355.3593
8.0000 390.6715 544.6144
8.0000 315.2286 847.8880
8.0000 164.8185 456.4721
8.0000 237.2573 989.3398
8.0000 77.1370 145.0098
8.0000 65.8827 900.7444
8.0000 44.8846 634.1563
8.0000 243.4290 766.9297
8.0000 210.9875 474.2046
8.0000 32.9418 815.5679
8.0000 15.9475 456.3964
9.0000 302.4733 770.5152
9.0000 72.2179 759.8410
9.0000 81.4730 564.7555
9.0000 58.4549 612.5564
9.0000 350.0827 145.9732
9.0000 65.8658 510.9172
9.0000 238.0324 755.4765
10.0000 436.1298 939.4800
10.0000 187.6726 898.4243
10.0000 161.9174 801.4695
10.0000 226.0228 254.1002
10.0000 60.2532 567.7775
10.0000 138.1312 763.0701
10.0000 63.5537 172.1847
10.0000 34.6960 785.1498
11.0000 32.1049 567.2452
11.0000 52.4174 593.4113
11.0000 139.1603 349.0192
11.0000 47.8173 736.1147
11.0000 113.0292 723.1275
11.0000 12.7127 541.9428
12.0000 187.7049 61.3127
12.0000 415.7316 504.8123
13.0000 57.3831 664.3180
14.0000 40.0112 373.3527
The first matrix has 74 rows, the second one 70 rows. Thank you so much.
Above are the values of circles found on a image using "imfindcircles". Here are the used grayscale images:
unfortunatly, ismember does not work, because the rows rarly are equal. do you know how to deal with xcorr2 ? would it be helpful?

Sign in to comment.

Products

Asked:

on 6 Nov 2012

Community Treasure Hunt

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

Start Hunting!