Re: Lists and rules

*To*: mathgroup at smc.vnet.net*Subject*: [mg66508] Re: Lists and rules*From*: "Ray Koopman" <koopman at sfu.ca>*Date*: Wed, 17 May 2006 03:30:04 -0400 (EDT)*References*: <e4bj1m$1b9$1@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

Thomas Schmelzer wrote: > Experts, > I would like to produce a decent piece of code for one of my projects. > I am constructing a list of sets > > S_1, S_2 and S_3 > > where S_j = { m , T[[1,m]] == j, m running from 1 to 100} > > T[[1,*]] is a row vector containing only 1, 2 and 3. > > I could setup a for loop and work with append. I have done that, but I > believe this approach is neither efficient nor elegant. Any hints for a > newbie? > > Best, > Thomas Is this what you want (but with 100 instead of 10)? In[1]:= t = Table[Random[Integer,{1,3}],{10}] Out[1]= {2,1,1,2,1,3,3,2,1,2} In[2]:= s = Table[#==j&/@t,{j,3}] Out[2]= {{False,True,True,False,True,False,False,False,True,False}, {True,False,False,True,False,False,False,True,False,True}, {False,False,False,False,False,True,True,False,False,False}} You could also do this: In[3]:= sa = SparseArray[Thread[Flatten[Table[{j,#}&/@ Flatten@Position[t,j],{j,3}],1]->True],{3,10},False] Out[3]= SparseArray[<10>,{3,10},False] In[4]:= sa == s Out[4]= True