Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1999
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1999

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

Search the Archive

RE:Help to clarify 'Map', 'Apply', and 'Thread'.

  • To: mathgroup at smc.vnet.net
  • Subject: [mg15709] RE:[mg15626] Help to clarify 'Map', 'Apply', and 'Thread'.
  • From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
  • Date: Fri, 5 Feb 1999 03:42:10 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

In response to a question from Wen-Feng Shaw I tried to demonstrate
'Map', 'Apply', 'Thread' and 'MapThread'.  In retrospect I don't like
the discussion I gave on MapThread.  I give it another attempt below.


*****MapThread******
MapThread can be used to Apply a function to each column of a matrix. 

In[1]:=
mat={{a,b,c,d,e,f},{1,2,3,4,5,6}};
MapThread[foo,mat]
Out[1]=
{foo[a,1],foo[b,2],foo[c,3],foo[d,4],foo[e,5],foo[f,6]}


MapThread is equivalent to using Map followed by Apply at level 2 (see
below).  I have to wonder why it isn't called ApplyThread.  If anyone
is interested I could talk about "level specification" at another time.

In[2]:=
MapThread[foo,mat]===Apply[foo,Thread[mat],2] Out[2]=
True

__________________

Consider the use of MapThread on the tensor below.

In[3]:=
tensor={
 {{a1,a2,a3,a4},{b1,b2,b3,b4}},
 {{c1,c2,c3,c4},{d1,d2,d3,d4}},
 {{e1,e2,e3,e4},{f1,f2,f3,f4}}
};
Dimensions[tensor]
Out[3]=
{3,2,4}


In[4]:=
MapThread[foo,tensor]
Out[4]={
foo[{a1,a2,a3,a4},{c1,c2,c3,c4},{e1,e2,e3,e4}],
foo[{b1,b2,b3,b4},{d1,d2,d3,d4},{f1,f2,f3,f4}]}


If you use MapThread on a tensor you may need to specify the level as a
third argument. In the next line MapThread is used at level 2 and gives
a very different result than the previous line.

In[5]:=
MapThread[foo,tensor,2]
Out[5]=
{{foo[a1,c1,e1],foo[a2,c2,e2],
  foo[a3,c3,e3],foo[a4,c4,e4]},
 {foo[b1,d1,f1],foo[b2,d2,f2],
  foo[b3,d3,f3],foo[b4,d4,f4]}}

___________________

The next line is an interesting use of MapThread.  Here we apply a list
of functions to a list of arguments.


In[6]:=
funcs={f1, f2, f3};
values={val1,val2,val3};
MapThread[(#1[#2])&, {funcs,values}] Out[6]=
{f1[val1],f2[val2],f3[val3]}

___________________

Alan Hayes once posted in this mathgroup something like the code below
to make a list of replacement rules.  Another interesting use of
MapThread.

In[7]:=
pos=Array[p,{4,3}]
Out[7]=
{{p[1,1],p[1,2],p[1,3]},
{p[2,1],p[2,2],p[2,3]},
{p[3,1],p[3,2],p[3,3]},
{p[4,1],p[4,2],p[4,3]}}

In[8]:=
posval={{1,2,3},{4,5,6},{7,8,9},{11,12,13}};
Flatten[MapThread[Rule,{pos,posval},2]]

Out[8]=
{p[1, 1] -> 1, p[1, 2] -> 2, 
  p[1, 3] -> 3, p[2, 1] -> 4, 
  p[2, 2] -> 5, p[2, 3] -> 6, 
  p[3, 1] -> 7, p[3, 2] -> 8, 
  p[3, 3] -> 9, p[4, 1] -> 11, 
  p[4, 2] -> 12, p[4, 3] -> 13}

__________________

Cheers,
Ted Ersek


  • Prev by Date: RE: Repeated calls to Mathematica process
  • Next by Date: Q: Why can't I stop TraditionalForm dismissing Times Attributes ?
  • Previous by thread: Re: Help to clarify 'Map', 'Apply', and 'Thread'.
  • Next by thread: Finding real part (newbie question)