Putting similar numbers into groups within an array
Show older comments
I have an array of numbers that looks something like follows. I want to group the array into subgroups where the numbers are all within 2 of each other. In this case, there would be 3 groups. Is there an easy way to do this? I need the method to be automated for work for arrays 100 entries long.
7340.1
7340.3
7340.6
7349.0
7349.4
7358.0
7358.1
7358.2
7358.7
% New groups would look like follows:
% Group 1
7340.1
7340.3
7340.6
% Group 2
7349.0
7349.4
% Group 3
7358.0
7358.1
7358.2
7358.7
Accepted Answer
More Answers (1)
Image Analyst
on 19 Jun 2017
1 vote
2 Comments
Alamgir M S M
on 10 Mar 2019
This is my Data-
[705.7142857 705.7142857 173.4285714 84.71428571 232.5714286 232.5714286 114.2857143 55.14285714 25.57142857 74.85714286 35.42857143 15.71428571 5.857142857 5.857142857].
I want to group same data into 1 group. This is 14*1 matrix where it has 3 pairs(705.7142857 705.7142857, 232.5714286 232.5714286 & 5.857142857 5.857142857) of same value data. So, I want these 3 pairs into 3 groups and rest of them into other 8 groups.
Is there anybody who can help me to code this?
Image Analyst
on 10 Mar 2019
Use unique() and setdiff(). On the remainder, use kmeans() to group into 8 groups. Should be easy. If you can't figure it out, let me know.
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!