MathGroup Archive 2006

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

Search the Archive

Re: Lists and rules


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


  • Prev by Date: Re: Lists and rules
  • Next by Date: NonlinearFit problem
  • Previous by thread: Re: Lists and rules
  • Next by thread: Re: Lists and rules