Split array into groups of close numbers
6 次查看(过去 30 天)
显示 更早的评论
Greetings,
Is there a built-in function in MATLAB to split an 1D array into groups of close numbers?
For example, I have an array
a = [1 2 3 10 11]
and want to split it into N = 2 groups. I expect to see something like
result{1} = [1 2 3]
result{2} = [10 11]
1 个评论
Dyuman Joshi
2023-12-8
How do you define close numbers?
Where the difference between adjacent numbers in 1?
采纳的回答
Stephen23
2023-12-8
a = [1,2,3,10,11];
n = 2;
x = kmeans(a(:),n)
c = accumarray(x,a(:),[],@(a){a})
0 个评论
更多回答(1 个)
Walter Roberson
2023-12-8
No -- because there is no rigourous meaning to what "close" means.
For example, in [1 3 5 7], surely 1 is not "close" to 7, but 5 is (probably) close to 7. Then 3 is "close" to 5, and 1 is "close" to 3. So are they all to be considered close as a group, even though 1 is "far" from 7 ?
Now, if the question were about dividing up into groups of consecutive values, then that would not be too difficult.
You could also use something like
N = 2;
a = [1 2 3 10 11]
[g, centroids] = kmeans(a(:), N)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!