Re: Lists and rules

• To: mathgroup at smc.vnet.net
• Subject: [mg66514] Re: Lists and rules
• From: Peter Pein <petsie at dordos.net>
• Date: Wed, 17 May 2006 03:30:36 -0400 (EDT)
• References: <e4bj1m\$1b9\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Thomas Schmelzer schrieb:
> 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
>
>
>
Hi Thomas,

I choose 10 instead of 100 to keep the output small:

T = Table[Random[Integer, {1, 3}], {3}, {10}];

and now assign S[n] the positions of n in T[[1]]:

Do[S[n] = Flatten[Position[T[[1]], n]], {n, 3}]

test:

T[[1]]
--> {2, 3, 2, 3, 3, 1, 2, 1, 3, 3}

Information["S", LongForm -> True]
--> Global`S
S[1] = {6, 8}

S[2] = {1, 3, 7}

S[3] = {2, 4, 5, 9, 10}

HTH,
Peter

```

• Prev by Date: Insulating data from code
• Next by Date: Re: what is K\$
• Previous by thread: Re: Lists and rules
• Next by thread: Re: Lists and rules