Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
finding neighboring cells in a large matrix

Subject: finding neighboring cells in a large matrix

From: Naresh Pai

Date: 3 Feb, 2011 02:59:05

Message: 1 of 6

I have a large binary matrix (4630 x 7040) consisting of 1's and 0's. I am wanting to find out the indices of neigboring cells of all 1's. I am using the FEX submission ixneighbors (File ID: #16991) to do this which requires the binary matrix and indices of 1's. as inputs However, when I pass the matrix the computer runs out of memory. Here is a simulated example:
----------------------------------------------------------------
% initiate a large binary matrix
large_matrix = ceil(rand(4630,7040));

% store indices of ones
one_Idx = find(large_matrix == 1);

% find neigboring indices
[ic,icd] = ixneighbors(large_matrix,One_Idx);
??? Out of memory. Type HELP MEMORY for your options.
----------------------------------------------------------------

One of the options I was considering was to use the function blockproc and send large_matrix in smaller blocks. I haven't yet been successful on that attempt. I was wondering if someone could help out or provide an alternative.

My ultimate goal is to use the neighboring cells indices and apply value of the cell containing 1's (in another corresponding matrix) to those neighboring cells. Sort of like the nibble function in ArcGIS, if anyone has used it.

Subject: finding neighboring cells in a large matrix

From: ImageAnalyst

Date: 3 Feb, 2011 03:43:52

Message: 2 of 6

You could use bwlabel or bwconncomp to find touching array elements.

Of your ultimate goal, it sounds like that could be accomplished very
simply by a single call to imdilate().

These are all in the Image Processing Toolbox.
Good luck,
ImageAnalyst

Subject: finding neighboring cells in a large matrix

From: Naresh Pai

Date: 3 Feb, 2011 07:08:03

Message: 3 of 6

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <78e4a31d-c4f3-4d6d-89da-12f9d0422a04@s11g2000yqh.googlegroups.com>...
> You could use bwlabel or bwconncomp to find touching array elements.
>
> Of your ultimate goal, it sounds like that could be accomplished very
> simply by a single call to imdilate().
>
> These are all in the Image Processing Toolbox.
> Good luck,
> ImageAnalyst

Thanks for your reply. I have been trying the imdilate function but it doesn't seem like I could get the desired effect out of it (or maybe I am not positioning it correctly). Say for e.g. I have a matrix

M = magic(4)

M =

    16 2 3 13
     5 11 10 8
     9 7 6 12
     4 14 15 1

% and I would like element (3,3) i.e. 6 be applied to all surrounding 8 elements. I tried


NHOOD = [0 0 0 0;0 0 0 0;0 0 1 0;0 0 0 0]


NHOOD =

     0 0 0 0
     0 0 0 0
     0 0 1 0
     0 0 0 0



% and then applied imdilate

imdilate(M,strel(NHOOD))

ans =

  -Inf -Inf -Inf -Inf
  -Inf 16 2 3
  -Inf 5 11 10
  -Inf 9 7 6

Obviously I am missing out something here. My desired answer would be:

M =

    16 2 3 13
     5 6 6 6
     9 6 6 6
     4 6 6 6

Subject: finding neighboring cells in a large matrix

From: ImageAnalyst

Date: 3 Feb, 2011 11:14:10

Message: 4 of 6

But M is not a binary matrix - it's an integer matrix. You'd have to
get a binary (logical) matrix first by doing
binaryImage = M == 6;
then use imdilate on that, something like (untested)
dilatedImage = imdilate(binaryImage);
then do assignment
outputImage = M
outputImage(dilatedImage) = 6;

Subject: finding neighboring cells in a large matrix

From: Sean de

Date: 3 Feb, 2011 13:40:04

Message: 5 of 6

"Naresh Pai" <npai@uark.edu> wrote in message <iid5lp$k8n$1@fred.mathworks.com>...
> I have a large binary matrix (4630 x 7040) consisting of 1's and 0's. I am wanting to find out the indices of neigboring cells of all 1's. I am using the FEX submission ixneighbors (File ID: #16991) to do this which requires the binary matrix and indices of 1's. as inputs However, when I pass the matrix the computer runs out of memory. Here is a simulated example:
> ----------------------------------------------------------------
> % initiate a large binary matrix
> large_matrix = ceil(rand(4630,7040));
>
> % store indices of ones
> one_Idx = find(large_matrix == 1);
>
> % find neigboring indices
> [ic,icd] = ixneighbors(large_matrix,One_Idx);
> ??? Out of memory. Type HELP MEMORY for your options.
> ----------------------------------------------------------------
>
> One of the options I was considering was to use the function blockproc and send large_matrix in smaller blocks. I haven't yet been successful on that attempt. I was wondering if someone could help out or provide an alternative.
>
> My ultimate goal is to use the neighboring cells indices and apply value of the cell containing 1's (in another corresponding matrix) to those neighboring cells. Sort of like the nibble function in ArcGIS, if anyone has used it.


Perhaps this neighborhood function won't kill your memory:
http://www.mathworks.com/matlabcentral/fileexchange/29330-neighbour-points-in-a-matrix

Subject: finding neighboring cells in a large matrix

From: Naresh Pai

Date: 3 Feb, 2011 16:27:03

Message: 6 of 6

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <iieb7k$s4c$1@fred.mathworks.com>...
> "Naresh Pai" <npai@uark.edu> wrote in message <iid5lp$k8n$1@fred.mathworks.com>...
> > I have a large binary matrix (4630 x 7040) consisting of 1's and 0's. I am wanting to find out the indices of neigboring cells of all 1's. I am using the FEX submission ixneighbors (File ID: #16991) to do this which requires the binary matrix and indices of 1's. as inputs However, when I pass the matrix the computer runs out of memory. Here is a simulated example:
> > ----------------------------------------------------------------
> > % initiate a large binary matrix
> > large_matrix = ceil(rand(4630,7040));
> >
> > % store indices of ones
> > one_Idx = find(large_matrix == 1);
> >
> > % find neigboring indices
> > [ic,icd] = ixneighbors(large_matrix,One_Idx);
> > ??? Out of memory. Type HELP MEMORY for your options.
> > ----------------------------------------------------------------
> >
> > One of the options I was considering was to use the function blockproc and send large_matrix in smaller blocks. I haven't yet been successful on that attempt. I was wondering if someone could help out or provide an alternative.
> >
> > My ultimate goal is to use the neighboring cells indices and apply value of the cell containing 1's (in another corresponding matrix) to those neighboring cells. Sort of like the nibble function in ArcGIS, if anyone has used it.
>
>
> Perhaps this neighborhood function won't kill your memory:
> http://www.mathworks.com/matlabcentral/fileexchange/29330-neighbour-points-in-a-matrix

Thanks again, IA and Sean. So I used a combination of what IA said and using euclidean distance. Say for e.g.

M = magic(6)

M =

    35 1 6 26 19 24
     3 32 7 21 23 25
    31 9 2 22 27 20
     8 28 33 17 10 15
    30 5 34 12 14 16
     4 36 29 13 18 11

% Just to demonstrate better lets make two elements the same:
M(5,5) = M(2,2)

M =

    35 1 6 26 19 24
     3 32 7 21 23 25
    31 9 2 22 27 20
     8 28 33 17 10 15
    30 5 34 12 32 16
     4 36 29 13 18 11

% Say my goal is to make elements neighboring cells containing 32 as 32
% First get a binary M

BinaryM = M == 32

BinaryM =

     0 0 0 0 0 0
     0 1 0 0 0 0
     0 0 0 0 0 0
     0 0 0 0 0 0
     0 0 0 0 1 0
     0 0 0 0 0 0

% Calculate the euclidean distance

EuclidM = bwdist(BinaryM)

EuclidM =

    1.4142 1.0000 1.4142 2.2361 3.1623 4.1231
    1.0000 0 1.0000 2.0000 3.0000 3.1623
    1.4142 1.0000 1.4142 2.2361 2.0000 2.2361
    2.2361 2.0000 2.2361 1.4142 1.0000 1.4142
    3.1623 3.0000 2.0000 1.0000 0 1.0000
    4.1231 3.1623 2.2361 1.4142 1.0000 1.4142

% Now use this to find neighbors

NeighborM = EuclidM > 0 & EuclidM < 1.5

NeighborM =

     1 1 1 0 0 0
     1 0 1 0 0 0
     1 1 1 0 0 0
     0 0 0 1 1 1
     0 0 0 1 0 1
     0 0 0 1 1 1

% Use these to update M

M(NeighborM) = 32

M =

    32 32 32 26 19 24
    32 32 32 21 23 25
    32 32 32 22 27 20
     8 28 33 32 32 32
    30 5 34 32 32 32
     4 36 29 32 32 32

% Thats the result I was expecting and it seems to work well for few trials on my 7040 x 4630 matrix too (for now)
Sean: If this approach gets slow, I will try the neighborhood FEX submission you have suggested. Thanks.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us