       • To: mathgroup at smc.vnet.net
• From: Allan Hayes <hay at haystack.demon.co.uk>
• Date: Sat, 8 Jun 1996 13:24:24 -0400
• Sender: owner-wri-mathgroup at wolfram.com

```Takuo Henmi <takuo at psy.uwa.edu.au>

asks how to replace each entry in a non-negative entry matrix with
the maximum of itself and its neighbors (diagonal neighbors
included).Original query attached

Takuo: here is one way

br[m_] := Join[{#},m,{#}]&[0&/@m[]];(*zero rows top and bottom*)
mm[m_] :=      				(*give result required*)
km[
Max,
{	RotateRight[#,{0,0}],
RotateRight[#,{1,0}],
RotateRight[#,{-1,0}],
RotateRight[#,{0,1}],
RotateRight[#,{0,-1}],
RotateRight[#,{1,1}],
RotateRight[#,{1,-1}],
RotateRight[#,{-1,1}],
RotateRight[#,{-1,-1}]
},
2
]&[bm[m]]
];

Check:

(mat = {{ 1, 2, 3},{2, 3, 4},{2,1,1}})//MatrixForm
1   2   3

2   3   4

2   1   1

mm[mat]//MatrixForm
3   4   4

3   4   4

3   4   4

The following version of mm is more compact:

temp = {{0,0},{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,1},{-1,1},{-1,-1}};
mm2[m_] :=
Max,
2
]];

****************
Original query

>From: Takuo Henmi <takuo at psy.uwa.edu.au>
>To: mathgroup at smc.vnet.net
>Organization: The University of Western Australia

My question is how to find the max. value of its neighbors in a
matrix, i.e.

if you have a matrix

1 2 3
2 3 4
2 1 1

(it'll always be a square matrix)

the max of the neighbors of cell [1,1] would be 3 (cell [2,2]), right?
so new cell [1,1] will be 3. Like that, you'll have a new matrix,
in this case,

3 4 4
3 4 4
3 4 4

(including the cell itself, e.g. cell [2,3]) and the boundaries are
all 0s.

Now I don't want to use like variable a[i,j], instead I wanna use
a={{...},...,{...}}