MathGroup Archive 1996

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

Search the Archive

Re: addressing matrix elements

  • To: mathgroup at
  • Subject: [mg3616] Re: [mg3566] addressing matrix elements
  • From: penny at (Des Penny)
  • Date: Thu, 28 Mar 1996 00:11:18 -0500
  • Sender: owner-wri-mathgroup at

>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.

>David Cabana

Hi David:
Try the following for the general function:

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

m= {{a11,a12,a13},{a21,a22,a23}};

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

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

{{Mod[a11, k], Mod[a12, k], Mod[a13, k]},
 {Mod[a21, k], Mod[a22, k], Mod[a23, k]}}

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

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

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

{{Mod[a11, k], Mod[a12, k], Mod[a13, k]},
 {Mod[a21, k], Mod[a22, k], Mod[a23, k]}}

f[{{22,33}, {44,55}},10]
{{2, 3}, {4, 5}}

Hope this helps.


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


  • Prev by Date: Re: packages
  • Next by Date: Re: Fourier? (Q)
  • Previous by thread: Re: addressing matrix elements
  • Next by thread: Re: addressing matrix elements