How to group 1's in a random matrix of 0's and 1's?

1 view (last 30 days)
Hi, i have a matrix of 0's and 1's that looks something like this when executed: [0 0 0 1 0 0; 1 1 0 1 1 0; 1 1 1 0 0 0; 1 0 1 1 1 0; 1 1 1 1 0 1; 0 0 1 0 1 1;]
This is the command I am using to execute this: x=round((1-0).*rand(6,6))
I was wondering if there is any way to be able to group the ones together so there isn't a one by itself such as for row 6 column 5? Thanks!

Answers (2)

John D'Errico
John D'Errico on 14 Apr 2015
Why? Does it get lonely all on its own? That is what it means to be random. You apparently have some requirements that need to be explained.
You can look for those events, and either delete the singleton ones, change a neighboring 0 to a 1, or replace an offending row when you see that happen.

Image Analyst
Image Analyst on 15 Apr 2015
You can expand each 1 with imdilate so that it's at least 2 - requires the Image Processing Toolbox.
out = imdilate(x, [1 1]);
That will also widen groups of 1's. If you don't want that then you have to isolate the single pixels with bwmorph and the 'clean' option, then dilate, then OR back in to the original image.

Categories

Find more on Random Number Generation 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!