# [Numpy-discussion] Adjacent matrix

Thiago Franco Moraes totonixsame@gmail....
Mon Jun 13 14:55:46 CDT 2011

```Hi all,

I'm reproducing a algorithm from a paper. This paper takes as input a
binary volumetric matrix. In a step from this paper, from this binary
volumetric matrix a adjacent matrix is calculated, thisadjacent matrix
is calculated as bellow:

"Find all of the grid points in that lie adjacent to one or more grid
points of opposite values."

This is the code used to calculate that matrix:

int x,y,z,x_,y_,z_, addr,addr2;

for(z = 1; z < nz-1; z++)

for(y = 1; y < ny-1; y++)

for(x = 1; x < nx-1; x++)

for(z_=z-1; z_ <= z+1; z_++)

for(y_=y-1; y_ <= y+1; y_++)

for(x_=x-1; x_ <= x+1; x_++)

{

addr = z*nx*ny+y*nx+x;

addr2 = z_*nx*ny+y_*nx+x_;

if(im[addr] != im[addr2])

out[addr] = 1;

}

Where nx, ny and nz are the x, y, z dimensions from the "im" input
binary matrix. I think this operation is called bwperim.

Yes, I can reproduce that code in Python. But it will not have a great
performance. My question is: Is there a way to do that using numpy or
scipy tools?

Thanks!

PS: Those indexing starts with 1 because it is a matlab arrays however
it is coded in C.
```

More information about the NumPy-Discussion mailing list