MathGroup Archive 1996

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

Search the Archive

Re: addressing matrix elements

  • To: mathgroup at
  • Subject: [mg3572] Re: addressing matrix elements
  • From: ianc (Ian Collier)
  • Date: Mon, 25 Mar 1996 21:34:52 -0500
  • Organization: Wolfram Research, Inc.
  • Sender: owner-wri-mathgroup at

In article <4itopk$v8 at>, drc at (David Cabana) wrote:

> The Mma language sometimes drives me crazy.  If I don't practice it
> regularly, I seem to forget everything.  Here's what I want to do, but
> can't.
> 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    drc at 

You can use Map to do this:

    func[ mat_, n_ ] := Map[ Mod[ #, n] &, mat ]

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

I hope this helps.


Ian Collier
Wolfram Research, Inc.
tel:(217) 398-0700   fax:(217) 398-0747    ianc at
Wolfram Research Home Page:


  • Prev by Date: mathematica-macppp conflict
  • Next by Date: Re: mathematica-macppp conflict
  • Previous by thread: addressing matrix elements
  • Next by thread: Re: addressing matrix elements