Re: apparently I don't know how to use Map (or Apply or MapThread)
- To: mathgroup at smc.vnet.net
- Subject: [mg77344] Re: [mg77290] apparently I don't know how to use Map (or Apply or MapThread)
- From: Sseziwa Mukasa <mukasa at jeol.com>
- Date: Thu, 7 Jun 2007 03:44:38 -0400 (EDT)
- References: <200706061117.HAA00225@smc.vnet.net>
On Jun 6, 2007, at 7:17 AM, dbsearch04 at yahoo.com wrote:
> I started with this:
>
> m = {{0.098, 0.951, 0.954}, {0.509, 0.914, 0.487}}
>
> I tried to do this
>
> Table[#[[i, i]], {i, Length[#]}] & /@ m
>
> It does not work, but this works
>
> Diag := Function[x, Table[x[[i, i]], {i, Length[x]}]]; Diag[m]
>
> !@#$%^&*(
>
> Can anyone tell me why? I also tried: Apply, MapThread,
> DiagonalMatrix, TakeMatrix, adding () around the pure function...
The arguments to the pure function in the first case are {0.098,
0.951, 0.954} and {0.509, 0.914, 0.487} since Map applies its first
argument to each element at level 1 by default. The second form you
are using applies the function to the zeroth level of its argument
which is m. An equivalent expression is:
Table[#[[i, i]], {i, Length[#]}] & /@ m
You can also do:
Tr[m,List]
Regards,
Ssezi
- References:
- apparently I don't know how to use Map (or Apply or MapThread)
- From: dbsearch04@yahoo.com
- apparently I don't know how to use Map (or Apply or MapThread)