Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: Re; Reordering lists?!
Replies: 6   Last Post: Aug 2, 1996 2:05 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Harald Berndt

Posts: 24
Registered: 12/7/04
Re: Re; Reordering lists?!
Posted: Jul 30, 1996 3:04 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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
_______________________________________________________________








Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.