Counting the number of adjacent binary points in a matrx
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I am writing a function to count the number of adjacent binary true values in a matrix, and the number of these clusters in a matrix. Adjacent points are connected at either at least one of the top, bottom or sides.
For example, the function run on this matrix would return:
cluster = [10]
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/154105/image.png)
as there are 10 points in a single cluster, and the function run on this matrix:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/154106/image.png)
would return
cluster = [1 1 1 1]
as there are 4 separate clusters, each with one point in. length(cluster) should equal the number of clusters in the image, and sum(cluster) == sum(A==1). Could I have some help in writing this function? I have thought to find the indices where the array = 1, then look to see if they are adjacent. It works in counting the single points, but it breaks down when counting the points in a larger cluster.
Minimal working example.
B = rand(10);
A = zeros(size(B));
A(B>0.9) = 1;
[row,col] = find(A(:,:,1)==1);
cluster = 1;
for i = 1:length(row)-1
count = abs(row(i+1)-row(i))<2 && abs(col(i+1)-col(i))<2;
if count == 1
cluster = cluster+count;
elseif count == 0
cluster = [cluster 1];
end
end
0 comentarios
Respuestas (0)
Ver también
Categorías
Más información sobre Large Files and Big Data en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!