Re: addressing matrix elements
- To: mathgroup at smc.vnet.net
- 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 wolfram.com
In article <4itopk$v8 at dragonfly.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,
>
> --
> David Cabana drc at gate.net
You can use Map to do this:
In[8]:=
func[ mat_, n_ ] := Map[ Mod[ #, n] &, mat ]
In[9]:=
func[{{22,33}, {44,55}}, 10]
Out[9]=
{{2, 3}, {4, 5}}
I hope this helps.
--Ian
-----------------------------------------------------------
Ian Collier
Wolfram Research, Inc.
-----------------------------------------------------------
tel:(217) 398-0700 fax:(217) 398-0747 ianc at wolfram.com
Wolfram Research Home Page: http://www.wolfram.com/
-----------------------------------------------------------
==== [MESSAGE SEPARATOR] ====