How to label the adjacent same value in a matrix

Hi,
Let's say I have a matrix like below:
A = [2 3 4 5 1 1 2 3 1 1 1 1;
8 5 1 1 1 1 5 4 1 0 1 1;
5 1 1 1 3 1 1 1 5 5 5 1;
1 1 1 2 1 1 2 1 1 1 6 8;
0 1 1 3 3 1 1 5 7 6 1 1]
I would like to know if there is any way to group and label every element if the adjacent(top, down, left, and right) value are equal to a specific value. For example, I want to group and label if the elements are equal to 1 in A. Thus, there are three groups, one of which is the largest on the left (24 elements), the second one is on the top-right corner (8 elements), and the third is on the bottom-right corner (only 2 elements). The result I expected is 3 groups with how many elements they have. Thanks in advance.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!