Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*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 2005

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

Search the Archive

Re: Cases and Nonatomic expression

  • To: mathgroup at smc.vnet.net
  • Subject: [mg56722] Re: Cases and Nonatomic expression
  • From: Curt Fischer <tentrillion at gmail.NOSPAM.com>
  • Date: Thu, 5 May 2005 06:01:07 -0400 (EDT)
  • References: <d59lfg$6i6$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Swati Shah wrote:
> Hi Everyone
> 
> I have a list
> k = {{1,1,2,2,3}, {1,2,2,2,3}, {2,3,3,3,3}, {5,2,2,2,1}, {6,1,1,1,1},
> {6,1,1,1,1}}
> 
> If the first element of the sublist is 1 I want to append the sublist to
> g1, if it starts with 2 then append g2 or starts with 6 then append to g6
> 
> I used cases and did the following:
> m1 = Cases[k, {1, __}];
> m2 = Cases[k, {2, __}];
> m3 = Cases[k, {3, __}];
> m4 = Cases[k, {4, __}];
> m5 = Cases[k, {5, __}];
> m6 = Cases[k, {6, __}];
> 
> However, instead of typing each one of these lines separately, it
> would be nice to use just a simple Map or something or a for loop (as
> I have more than 50 different start values)

[...]

> However outside the for loop the values of g1..g6 is empty.
> 
> b) I tried using MAP (in the similar way as the for)
> 
> But I get the following error:
> Append::normal: Nonatomic expression expected at position 1 in Append[g1,{1, \
> 168, 0.695873, 6.54462, 62.4578, 82.5056}]
In[1]:=
k = {{1, 1, 2, 2, 3},
     {1, 2, 2, 2, 3},
     {2, 3, 3, 3, 3},
     {5, 2, 2, 2, 1},
     {6, 1, 1, 1, 1},
     {6, 1, 1, 1, 1}};

You can Map[] Cases[] onto your domain of interest, which in the test 
problem was {1,2,3,4,5,6}, by using the command below.  This creates a 
vector m whose elements are things that will later be appended to the 
corresponding elements of g.

In[2]:=
m = (Cases[k, {#1, ___}] & ) /@ Range[1, 6]

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

I made up an initialization for your list g.  You could use empty lists 
too if you wanted.

In[3]:=
g = {{foo1}, {foo2}, {foo3},
     {foo4}, {foo5}, {foo6}};

Then, I mapped the pure function AppendTo[g[[#]],m[[#]]] onto the domain 
of this problem to form the updated g.

In[4]:=
(AppendTo[g[[#1]],
      m[[#1]]] & ) /@
    Range[1, 6];
In[5]:=
ColumnForm[g]

Out[5]=
{foo1, {{1, 1, 2, 2, 3}, {1, 2, 2, 2, 3}}}
{foo2, {{2, 3, 3, 3, 3}}}
{foo3, {}}
{foo4, {}}
{foo5, {{5, 2, 2, 2, 1}}}
{foo6, {{6, 1, 1, 1, 1}, {6, 1, 1, 1, 1}}}

Is this what you wanted?

-- 
Curt Fischer


  • Prev by Date: Re: books on writing packages
  • Next by Date: Re: letrec/named let
  • Previous by thread: Re: Cases and Nonatomic expression
  • Next by thread: how call a function by same name in 2 different contexts?