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

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

Search the Archive

Re: Re; Reordering lists?!

  • To: mathgroup at smc.vnet.net
  • Subject: [mg4545] Re: Re; Reordering lists?!
  • From: Harald Berndt <haraldb at nature.berkeley.edu>
  • Date: Wed, 7 Aug 1996 04:17:51 -0400
  • Organization: University of California Forest Products Lab
  • Sender: owner-wri-mathgroup at wolfram.com

Andre and Alain wrote:
 
> <The code that replaces them>
> 
> Do[If[gsub[[n]]==1,gsub[[n]]=4]||
> If[gsub[[n]]==2,gsub[[n]]=3]||
> If[gsub[[n]]==3,gsub[[n]]=2]||
> If[gsub[[n]]==4,gsub[[n]]=1],
> {n,1,Length[gsub]}]
> 
> <It doesn't work properly?!>
> 

No, of course that wouldn't work properly. When your code gets to the 
first instance of 2, it replaces it with 3, which is then replaced by 
2 by the next statement (so, no change at all). On the other hand, all 
original 3's will end up as 2's. Ditto with the 4's and 1's: they'll 
all be 1, by the time your logic statement is through. You'll need 
"placeholders."

Besides, the Do[] and long logic statements are not very efficient use 
of Mma. The Mma way of doing this would be with pattern matching and 
replacement rules:

In[12]:=
chngdData =
((((((data//.1->x)//.4->1)//.3->y)//.2->3)//.y->2)//.x->4)
Out[12]=
{3, 4, 4, 4, 4, 3, 4, 3, 4, 4, 4, 3, 3, 4, 4, 2, 4, 4, 3, 4, 3, 4, 3, 
 
  4, 4, 4, 4, 3, 4, 3, 4, 2, 4, 3, 3, 4, 3, 4, 3, 2, 3, 1, 4, 4, 4, 4, 
 
  3, 4, 4, 4, 4, 3, 3, 3, 4, 3, 4, 3, 2, 3, 3, 3, 4, 3, 4, 3, 3, 3, 3, 
 
  3, 3, 4, 4, 4, 4, 4, 4, 3, 3, 3, 4}
In[11]:=
Frequencies[chngdData]
Out[11]=
{{1, 1}, {4, 2}, {34, 3}, {42, 4}}
-- 
_______________________________________________________________
Harald Berndt                          University of California
Research Specialist                  Forest Products Laboratory
Phone: 510-215-4224                            FAX:510-215-4299
_______________________________________________________________


==== [MESSAGE SEPARATOR] ====


  • Prev by Date: Re: Display Region Programmable ?
  • Next by Date: Minors[]
  • Previous by thread: Re: Re; Reordering lists?!
  • Next by thread: Re: Re; Reordering lists?!