Re: Thread function over matrix elements?
- To: mathgroup at smc.vnet.net
- Subject: [mg69887] Re: Thread function over matrix elements?
- From: dh <dh at metrohm.ch>
- Date: Tue, 26 Sep 2006 05:21:43 -0400 (EDT)
- References: <efacnv$1v0$1@smc.vnet.net>
Hi Ben,
you must understand level specifications, then all becomes clear:
n indicates level 1..n, {n} indicates level n
therefore, what you want is: Map[f, mat, {2}], if you say: Map[f, mat,
2], f is appled to level 1 and 2, hardly what you want.
Apply does not work, because you can not replace the head of an atom.
Neither does Thread because Thread has no level-control.
Daniel
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
>