Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2006

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

Search the Archive

Re: Lists and rules

  • To: mathgroup at smc.vnet.net
  • Subject: [mg66490] Re: [mg66485] Lists and rules
  • From: Adriano Pascoletti <pascolet at dimi.uniud.it>
  • Date: Wed, 17 May 2006 03:29:16 -0400 (EDT)
  • References: <200605160349.XAA01113@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Thomas,
this creates a random list of m integers between 1 and k:

In[2]:=
k=3;m=15;SeedRandom[6516];
T=Table[Random[Integer,{1,k}],{m}]

Out[2]=
{3,1,1,3,3,2,3,3,2,1,3,2,3,1,1}

and this assigns to S[i] the flattened list of positions of i in T  
(1²i²k)

In[7]:=
Scan[(S[#]=Flatten@Position[T,#])&,Range[k]]

The sets are

In[8]:= S[1]
Out[8]= {2,3,10,14,15}

In[9]:= S[2]
Out[9]= {6,9,12}

In[10]:= S[3]
Out[10]= {1,4,5,7,8,11,13}

This approach requires linear (i.e. O(m)) time and space.
Hope it helps
Adriano Pascoletti

On 16 mag 2006, at 05:49, 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
>
>
>


  • References:
  • Prev by Date: Re: Reconstructing data points from a InterpolatingFunction object
  • Next by Date: Re: Lists and rules
  • Previous by thread: Lists and rules
  • Next by thread: Re: Lists and rules