Re: Cases and Nonatomic expression
- To: mathgroup at smc.vnet.net
- Subject: [mg56734] Re: [mg56718] Cases and Nonatomic expression
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Thu, 5 May 2005 06:01:17 -0400 (EDT)
- References: <200505040434.AAA06285@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On 4 May 2005, at 13:34, 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) > > I tried the following 2 ways: > > a) using a for loopFor [i = 1, i < 7, > t = "g" <> ToString [i]; > Print [t]; > t = Cases [k, {i, __}]; > Print [t] > i++] > > 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}] > > > Any suggestions as to how I can get this working? > > Thanks in advance > > Swati > > One way: 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}}; In[2]:= With[{l=Split[k,#1[[1]]=#2[[1]]&]},Evaluate[ ToExpression["m"<>ToString[#]]&/@Range[Length[l]]]=l]; Now m1 {{1, 1, 2, 2, 3}, {1, 2, 2, 2, 3}} etc. But actually I can't see the point using names like m1 etc. You can make it all much simpler if instead you use m[1] etc. Andrzej Kozlowski Chiba, Japan http://www.akikoz.net/andrzej/index.html http://www.mimuw.edu.pl/~akoz/
- References:
- Cases and Nonatomic expression
- From: Swati Shah <swatshah@gmail.com>
- Cases and Nonatomic expression