MathGroup Archive 2006

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

Search the Archive

Re: Thread function over matrix elements?


If f is already a function that has attribute Listable, then you need 
only use:

   f[mat]

For example, Sin[mat].  And if f does not have attribute Listable, you 
could give it that attribute:

   SetAttributes[f, Listable]
   mat=Table[i+j,{i,0,2},{j,0,2}]
   f[mat]
{{f[0],f[1],f[2]},{f[1],f[2],f[3]},{f[2],f[3],f[4]}}

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
> 
> 

-- 
Murray Eisenberg                     murray at math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower      phone 413 549-1020 (H)
University of Massachusetts                413 545-2859 (W)
710 North Pleasant Street            fax   413 545-1801
Amherst, MA 01003-9305


  • Prev by Date: Re: why does FindMinimum have the attribute HoldAll?
  • Next by Date: "the sum of squares removed by fitting..."?
  • Previous by thread: Re: Thread function over matrix elements?
  • Next by thread: Re: Thread function over matrix elements?