       • To: mathgroup at smc.vnet.net
• Subject: [mg3616] Re: [mg3566] addressing matrix elements
• From: penny at edu-suu-scf.sc.suu.edu (Des Penny)
• Date: Thu, 28 Mar 1996 00:11:18 -0500
• Sender: owner-wri-mathgroup at wolfram.com

```>Say M is a matrix of integers.  I want to func[M, k] to return a matrix
>same dimensions as M, each entry consisting of the corresponding entry of
>M taken modulo k.  For instance, func[{{22,33}, {44,55}}, 10] should
>return {{2,3}, {4,5}}.  I would like this to work for arbitrary
>rectangular integer matrices, but am not having much luck.  It seems like
>this should be easy,  but I'm stumped.
>
>More generally, I would like to be able to apply a function f to each
>element in a matrix of arbitrary size and dimensions, without worrying
>about the particulars of the matrix representation via lists.  I want
>func[M, g] to return a matrix of the same size and shape as M, with
>elements formed by applying g to corresponding elements of M.  Is there
>nice way to do this?  Seems like some combination  of Map, Apply, Thread,
>SetAttributes Listable, Outer, etc. could do the job, but I am lost in the
>morass of possibilites.   Any help would be appreciated.
>
>Thanks,

>David Cabana

Hi David:
Try the following for the general function:

In:=
func[m_,g_]:= Map[g,m,{2}]

m= {{a11,a12,a13},{a21,a22,a23}};
func[m,g]

Out=
{{g[a11], g[a12], g[a13]}, {g[a21], g[a22], g[a23]}}

We can now use this function for your particular case as follows:

In:=
func[m,Mod[#,k]&]
Out=
{{Mod[a11, k], Mod[a12, k], Mod[a13, k]},
{Mod[a21, k], Mod[a22, k], Mod[a23, k]}}

In:=
func[{{22,33}, {44,55}},Mod[#,10]&]
Out=
{{2, 3}, {4, 5}}

If you want a function in the same format as you propose for Mod you could use:

In:=
f[m_,k_] := Map[Mod[#,k]&,m,{2}]

In:=
f[m,k]
Out=
{{Mod[a11, k], Mod[a12, k], Mod[a13, k]},
{Mod[a21, k], Mod[a22, k], Mod[a23, k]}}

In:=
f[{{22,33}, {44,55}},10]
Out=
{{2, 3}, {4, 5}}

Hope this helps.

Cheers,

Des Penny

==========================
Des Penny
Physical Science Dept.
Southern Utah University
Cedar City, UT 84720

VOICE: (Office): (801) 586-7708
(Home)  : (801) 586-2286
FAX:    (801) 865-8051
e-mail: penny at suu.edu
==========================

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

```

• Prev by Date: Re: packages
• Next by Date: Re: Fourier? (Q)