How to create Nearest Neighbors for a matrix of 1's and 0's?

I have a matrix of 1's and 0's.
mat =
10100
11101
11011
I would like to create neighbors to this matrix where the 1's remain consecutive (i.e. they follow each other continuously) and in the same respective column . I've tried an offset mask but it does not work for the end row of the matrix since it breaks apart the group of 1's. I believe padding to fix this will add irrelevant data to the matrix?

Answers (1)

I don't know what that means. Do you mean a morphological dilation where you just grow out all 1's by a layer to the left and right? If so, use imdilate. Like:
outputImage = imdilate(mat, [1,1,1]);
If not, then pick a smaller example and say what you want the output to be, like you want it to be a matrix of the same size, or you want a cell array where each cell holds the row and column indexes of the neighbors of that pixel or whatever. Perhaps if would help if you said what you want to achieve. Let's say you had whatever you're thinking of. Then what?

3 Comments

Sorry about that.
Say i had a matrix
10100
11101
11011
I would like to create a set of similar matrices (nearest neighbors) to this. Constrictions:
  1. All matrices must be the same size. In this eg 3x5
  2. In each column, the 1's must be consecutive e.g. column 3 could be [1;1;0] or [0;1;1] but never [1;0;1]. Essentially the group of 1's in the columns will be shifted up or down but never separated.
  • Data is never exchanged between columns. So if there is three 1's in a particular column, it can never have one/two 1's in any of the neighbor matrices.
You forgot to give the "answer" for this small example, and to give us the big picture about why you need this (what you would do with the answer).
My problem is to create a tabu algorithm with the above matrix as the initial solution. I'm at the point where i need to generate neighbor solutions to this initial solution. The answer will be a set of matrices with the above constrictions. For eg. one of the neighbor solutions could be
1101
1111
1010

Sign in to comment.

Asked:

on 15 Feb 2015

Commented:

on 15 Feb 2015

Community Treasure Hunt

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

Start Hunting!