How can I identity clusters of points in a vector?

11 views (last 30 days)
I plotted a vector and its easy to see that there are distinct groups of points, as shown in the following pictures. How can I make matlab give me the intervals regarding each set of nearby points?
Note: It's extremely important for me to do this, and I don't mind if the answer is not a "works at all cases" type of answer. I only want to confirm what can be seen visually.

Accepted Answer

Image Analyst
Image Analyst on 9 Jan 2016
This is really easy if you have the Image Processing Toolbox. You just need to threshold to identify non-zero elements, then call bwareaopen to get rid of stretches that are closer than the closest you will allow elements to be before they are considered as the same region, then call bwlabel. So it's like 3 lines of code or so
% Get a logical vector that says which elements have non-zero data in them.
nonZeroElements = data ~= 0;
% Define the closest regions can be. If they are farther away than this,
% then they will be considered as separate regions.
minSeparation = 10;
nonZeroElements = ~bwareaopen(~nonZeroElements, minSeparation);
[labeledRegions, numRegions] = bwlabel(nonZeroElements);
labeledRegions is a labeled array with the same number of elements as your data. Each element will say whether that element belongs to region 1, region 2, region 3, etc.
Would that work for you?
  1 Comment
varjak
varjak on 9 Jan 2016
Thank you, I found out that I have the Image Processing Toolbox and it worked perfectly. I must say that in the past weeks, I also read some of your other answers in this forum (mainly about thresholding) and they helped me out greatly. Best Regards!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!