Re: addressing matrix elements
- To: mathgroup at smc.vnet.net
- Subject: [mg3585] Re: addressing matrix elements
- From: pecora at zoltar.nrl.navy.mil (Lou Pecora)
- Date: Wed, 27 Mar 1996 03:26:15 -0500
- Organization: Naval Research Lab
- Sender: owner-wri-mathgroup at wolfram.com
In article <4itopk$v8 at dragonfly.wolfram.com>, drc at emi.net (David Cabana) wrote: > 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. How about func[M_,k_] := Map[Mod[#,k]&,M,{2}] ? The {2} tells Map to apply the "pure function" Mod[#,k]& at level 2, the matrix component level in your case. Pure functions are worth learning about. Should generalize easily to your other matrix functions. -- Lou Pecora code 6341 Naval Research Lab Washington DC 20375 USA pecora at zoltar.nrl.navy.mil /* My views are not those of the U.S. Navy. If you want their views, you have to go to war. */ ==== [MESSAGE SEPARATOR] ====