Finding clusters in 1D array that contains certain number of entries with specific value
5 次查看(过去 30 天)
显示 更早的评论
Hello,
I am wondering what would be the efficient way to find clusters of data in 1D array that contains certain number of specific value. For instance, I have a binary array that looks like this
data = [0 0 0 0 1 0 1 0 0 1 0 0 0 0 0 1 0 0 0 1 1 0 1 0 0 0 0 1 1 1 1 0 1 0 0 0 1 0 1]'
I would like to find a cluster that satisfy the following conditions
1) length of the cluster is fixed as 'L' (let's say L=7)
2) cluster should contain at least 'p' number of ones (let's say p=3)
3) if some clusters overlap or are located right next to each other, they can merge to create one bigger cluster
After clustering, I should get the following answer (answers are marked with {})
data = [0 0 0 {0 1 0 1 0 0 1 0} 0 0 0 {0 1 0 0 0 1 1 0 1 0 0 0 0 1 1 1 1 0 1 0} 0 0 1 0 1]'
Here, you can see some clusters merged to form a bigger cluster.
In the end, I need to know the index of the start and end point of the each cluster as well as the total number of clusters. This will give me the following answers.
1) number of clusters = 2
2) index of initial value in each cluster = [4 15]
2) index of last value in each cluster = [11 34]
The actual array I need to work with is quite large so I want to see if there's an efficient way to compute this.
Thank you in advance!
11 个评论
Image Analyst
2020-4-22
编辑:Image Analyst
2020-4-22
I'm afraid I still don't understand. Perhaps you can use cumsum() to count the 1's as you go along:
data = [0 0 0 0 1 0 1 0 0 1 0 0 0 0 0 1 0 0 0 1 1 0 1 0 0 0 0 1 1 1 1 0 1 0 0 0 1 0 1]
c = cumsum(data)
So why aren't the final 101 in a cluster? Is it because somehow cluster 2 and 3 got merged to form one giant cluster of more than 7 elements and there just weren't 7 elements remaining after that to form another cluster?
And why does cluster 1 have 8 elements instead of 7? And why is cluster #1 {0 1 0 1 0 0 1 0} instead of {0 0 1 0 1 0 0 1} or {1 0 1 0 0 1 0 0}
And why do you need to do this quirky thing anyway? What is the real world use case?
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!