Stuart’s MATLAB Videos

Watch and Learn

Puzzler: Find largest connected island

Every now and then we send MATLAB challenges through the department to help sharpen our programming skills. What amazes me is the sheer variety of solutions that people come up with.

Today, I have a challenge for the community here. Given a matrix of random, positive, whole numbers, find the largest group of identical, four connected values.

If there are two groups tied for the same size, choose the largest valued. If there is still a tie, it is a free choice which to use. Simply return a binary matrix with ones where the island of connected pixels is.

island.jpg

I suspect that this challenge will be much easier for people with access to the Image Processing Toolbox. I am curious to see what kind of creative solutions people will find with and without the toolbox.

Here is the starter code:


n = 10; % size of matrix
v = 9; % max value of matrix, different values give bigger islands
a = round(rand(n)*v);
flag = mySolver(a); % mySolver will return a binary matrix

Your mission, write mySolver.m and place it in the comment section.

When you post your code in the comments, please use the tags


<pre> <code>

all the code so someone can just copy and paste it from the comments.

</code> </pre>
|
  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.