Re: addressing matrix elements
- To: mathgroup at smc.vnet.net
- Subject: [mg3584] Re: addressing matrix elements
- From: Jorma.Virtamo at vtt.fi (Jorma Virtamo)
- Date: Wed, 27 Mar 1996 03:26:04 -0500
- Sender: owner-wri-mathgroup at wolfram.com
drc at emi.net (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, > In general you have to make g listable: SetAttributes[g,Listable] Then, In[] := g[{{22,33}, {44,55}}] Out[] = {{g[22], g[33]}, {g[44], g[55]}} The Mod-function is listable, so it works directly: In[] := Mod[{{22,33}, {44,55}},10] Out[] = {{2, 3}, {4, 5}} -- Jorma Virtamo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Jorma Virtamo VTT Information Technology phone: +358 0 456 5612 Telecommunications fax: +358 0 455 0115 P.O. Box 1202 email: jorma.virtamo at vtt.fi FIN-02044 VTT web: http://www.vtt.fi/tte/ Finland ==== [MESSAGE SEPARATOR] ====