MATLAB: Segregate various elements of a matrix into separate groups based on their close proximity

grouping elements in a matrix into separate groupsStatistics and Machine Learning Toolbox

Hello, I have a 2D matrix that I have created from filtering a contour map of an image and I created a regional maxima of the final image as shown in the attached photo. now the matrix contains only ones and zeros in a random order. I need now to group all the points with "ones" that are in close proximity into a single group and know the raw and column matrix indexes of the first element of each group. this to be applied to all groups. I do not want to do this manually, as I want to apply this algortihm to thousands of matrices that have their regional maxima distributed randomly. I need to know if there is a certain function/exchange file in matlab that can manage to segregate these groups. please find attached photo for more explanation of what I need. Thanks in advance.

Best Answer

Do you know how many groups there will be? If so, just get the x and y coordinates of each point using find(), then pass them into kmeans().
If you don't know then you could use some other clustering technique, like dbscan.
Or you could use pdist2() and cluster that by thresholding. It would be simple but it would probably require that the groups be well separated (unlike dbscan).
There are other unsupervised clustering methods that you can use. Take a deeper dive here: https://www.mathworks.com/help/stats/cluster-analysis.html
Related Question