Search All of the Math Forum:

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

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

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

 Messages: [ Previous | Next ]
 Harald Berndt Posts: 24 Registered: 12/7/04
Re: Re; Reordering lists?!
Posted: Jul 30, 1996 3:04 AM

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
_______________________________________________________________

Date Subject Author
7/28/96 Andre and Alain
7/30/96 Stephen P Luttrell
7/30/96 Elizabeth J Earhart
7/30/96 Harald Berndt
7/30/96 Harald Berndt
7/30/96 Wolfram Jr.
8/2/96 Hein Hundal