Re: Thread function over matrix elements?
- To: mathgroup at smc.vnet.net
- Subject: [mg69880] Re: Thread function over matrix elements?
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Tue, 26 Sep 2006 05:21:27 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <efacnv$1v0$1@smc.vnet.net>
ben wrote: > Dear all, > > how do I thread a function over all elements of a matrix? > This piece of code works, but I doubt its the way i am supposed to do > this > > mat=Table[i+j,{i,0,2},{j,0,2}] > > Map[Map[f[#]&,#]&,mat] > > I tried > > Map[f[#]&,mat,2] > Apply[f[#]&,mat,2] > Thread[f[mat]] > > but they give weird results. > > Cheers > Ben > Hi Ben, You must enclose the specification level with curly braces to tell the function Map to work only at the specified level, say level 2, and not from level one up to level two. In[1]:= mat = Table[i + j, {i, 0, 2}, {j, 0, 2}]; In[2]:= Map[f, mat, {2}] Out[2]= {{f[0], f[1], f[2]}, {f[1], f[2], f[3]}, {f[2], f[3], f[4]}} Regards, Jean-Marc