MathGroup Archive 1996

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Please Help

  • To: mathgroup at smc.vnet.net
  • Subject: [mg4159] Re: [mg4093] Please Help
  • 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>
in [mg4093]  Please Help

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[[1]]];(*zero rows top and bottom*)
bm[m_] := Thread[br[Thread[br[m]]]]; (*border with zeros*)
km[m_] := Thread[Take[Thread[Take[m,{2,-2}]],{2,-2}]](*drop border *)
mm[m_] :=      				(*give result required*)
	km[
		MapThread[
			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_] :=
   km[MapThread[
      Max,
      Apply[RotateRight, Thread[{bm[m],temp}, List, {-1}],{1}],
      2
   ]];

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

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

For those mathematica wizs, please help me.

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={{...},...,{...}}

PLease help me if you would.

Thanks in advance.

Takuo Henmi


==== [MESSAGE SEPARATOR] ====


  • Prev by Date: New Diff Eq solver
  • Next by Date: Re: No ListPlot points shown
  • Previous by thread: Re: Please Help
  • Next by thread: Re: Dirac Delta Function