Re: ListCorrelate[] ??

• To: mathgroup at smc.vnet.net
• Subject: [mg30128] Re: [mg30104] ListCorrelate[] ??
• From: Daniel Lichtblau <danl at wolfram.com>
• Date: Sun, 29 Jul 2001 21:26:19 -0400 (EDT)
• References: <200107280551.BAA08392@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Brent Pedersen wrote:
>
> with a real square matrix H, I calculate the sum of each cell and its 8
> neighbors as:
> Htot=ListCorrelate[kern, H ,{2,-2}];     (thanks to the help of some of
> you)
> where:
> kern = Table[1, {3}, {3}];
>
> now, for each 9 cell neighborhood around (and including cell H[[i,j]], I
> wish to calculate:
> Sum[(H[[m,n]]/Htot[[i,j]])^c,{m,i-1,i+1},{n,j-1,j+1}];
> (when c=1, this will be 1.)
> of course, this code won't work as is but the idea is there.I can write
> loops containing a Sum[] similar to that above,but i am sure that
> ListCorrelate[] operates much faster (which is important since H is
> 256*256).in short, i need to output a 256*256 matrix where each cell
> contains the result of the Sum[] function above.
>
> ListCorrelate[kern, H/Htot,{2,-2}]; does not work because it contains
> H[[i,j]]/Htot[[i,j]]  not H[[i,j]]/Htot[[m,n]];
>
> will some variation of
> ListCorrelate[H, 1/Htot, {n,-n}]; work?
> as yet, no luck with that.
> any ideas will be greatly appreciated.
> thanks,
> -brent

It appears you want to take the correlated cth power of your matrix
divided by the cth power of the correlated matrix. This may be done as
below.

size = 256;
c = 2;
hmat = Table[Random[], {size}, {size}];
kernel = Table[1, {3}, {3}];

In[35]:= Timing[
hcorr = ListCorrelate[kernel, hmat, {2,-2}];
hpowercorr = ListCorrelate[kernel, hmat^c, {2,-2}];
result = hpowercorr / hcorr^c;
]
Out[35]= {1.22 Second, Null}

The above uses cyclic repition of the second argument at the boundaries.
If you are doing image processing then this would not be desirable
(unless your picture came off a bagel). In that case a reasonable thing
might be to border your array with copies of the first and last rows and
columns. This could be accomplished as below.

<<LinearAlgebra`MatrixManipulation`

borderMatrix[mat_?MatrixQ] := With[
{newmat=AppendColumns[{First[mat]},mat,{Last[mat]}]},
AppendRows[Transpose[{newmat[[All,1]]}],newmat,
Transpose[{newmat[[All,Length[newmat[[1]]]]]}]]
]

In[36]:= borderMatrix[Array[a, {3,3}]] // MatrixForm

Out[36]//MatrixForm= a[1, 1]   a[1, 1]   a[1, 2]   a[1, 3]   a[1, 3]

a[1, 1]   a[1, 1]   a[1, 2]   a[1, 3]   a[1, 3]

a[2, 1]   a[2, 1]   a[2, 2]   a[2, 3]   a[2, 3]

a[3, 1]   a[3, 1]   a[3, 2]   a[3, 3]   a[3, 3]

a[3, 1]   a[3, 1]   a[3, 2]   a[3, 3]   a[3, 3]

Daniel Lichtblau
WOlfram Research

```

• Prev by Date: Two factors of (10^71-1)/9 = R71
• Next by Date: Re: Transferring graphics from Mathematica to MS Word
• Previous by thread: ListCorrelate[] ??
• Next by thread: subscripted variables in Modules