Re: Lists and rules
- To: mathgroup at smc.vnet.net
- Subject: [mg66512] Re: Lists and rules
- From: "Ray Koopman" <koopman at sfu.ca>
- Date: Wed, 17 May 2006 03:30:32 -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 Same t as my previous post, same way to get s. In[1]:= m = Length[t = {2,1,1,2,1,3,3,2,1,2}] Out[1]= 10 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}} But a better way to get sa. In[3]:= sa = SparseArray[Thread[Transpose@{t,Range@m}->True], {3,m},False] Out[3]= SparseArray[<10>,{3,10},False] In[4]:= sa == s Out[4]= True