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 ]
 Elizabeth J Earhart Posts: 10 Registered: 12/7/04
Re: Re; Reordering lists?!
Posted: Jul 30, 1996 3:02 AM

In article <4thaug\$j6e@dragonfly.wolfram.com>,
Andre and Alain <abindon@gpu.srv.ualberta.ca> wrote:
> Hey! I have a list of Numbers, 1 to 4, and wish to rescale them so
>that 1 replkaces 4 and 3 replaces 2 and 2 replaces 3 etc. I made a little
>code to do this on an element per element basis and it doesn't seem to do
>it! Could someone let me know what I'm doing wrong or whether it's a Mma
>problem? Thanks
>

[nonworking code snipped]

The problem is that when you encounter 1's or 2's, you're changing them
into 4's or 3's respectively, and then you hit the next If[] statement
and change them back again!

The easiest way to accomplish what you want is

newgsub = (5-#)& /@ gsub;

('/@' is shorthand for Map[], '(5-#)&' is a pure function equivalent
to f[x_] := (5-x); ).

If you're not always going to be doing your substitution in such a
way that the numbers add up to a constant, you can define an encoding
function as follows:

encode[x_] := Switch[x,
1, 4,
2, 3,
3, 2,
4, 1,
_, x];

(That last line isn't absolutely necessary - it leaves unchanged anything
that doesn't match any of the defined cases).

Then you will get the correct result if you do

newgsub = encode /@ gsub;

(Equivalently, newgsub = Map[encode, gsub, {1}];)

-Elizabeth

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