MathGroup Archive 2006

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

Search the Archive

Re: Lists and rules

  • To: mathgroup at
  • Subject: [mg66512] Re: Lists and rules
  • From: "Ray Koopman" <koopman at>
  • Date: Wed, 17 May 2006 03:30:32 -0400 (EDT)
  • References: <e4bj1m$1b9$>
  • Sender: owner-wri-mathgroup at

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},

But a better way to get sa.

In[3]:= sa = SparseArray[Thread[Transpose@{t,Range@m}->True],

Out[3]= SparseArray[<10>,{3,10},False]

In[4]:= sa == s

Out[4]= True

  • Prev by Date: Re: want real solutions instead of complex ones
  • Next by Date: Re: Reconstructing data points from a InterpolatingFunction object
  • Previous by thread: Re: Lists and rules
  • Next by thread: Re: Lists and rules