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 ]
 Wolfram Jr. Posts: 2 Registered: 12/11/04
Re: Re; Reordering lists?!
Posted: Jul 30, 1996 3:16 AM

In article <4thaug\$j6e@dragonfly.wolfram.com>, abindon@gpu.srv.ualberta.ca
(Andre and Alain) 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
>
> <The original list>
>
> gsub=Flatten[ColumnTake[gcsq,{4}]]
> {2, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 2, 2, 1, 1, 3, 1, 1, 2, 1, 2, 1, 2, 1, 1,
> 1, 1, 2, 1, 2, 1, 3, 1,
> 2, 2, 1, 2, 1, 2, 3, 2, 4, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 2, 2, 1, 2, 1, 2,
> 3, 2, 2, 2, 1, 2, 1,
> 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1}
>
> <Frequencies of the list>
>
> Frequencies[gsub]
> {{42, 1}, {34, 2}, {4, 3}, {1, 4}}
>
> <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?!>
>
> Frequencies[gsub]
> {{43, 1}, {38, 2}}
>
> Of course the new list frequencies should be exactly opposite than the first
> Thanks
> Andre Bindon
> Ablerta Hospital Edmonton

You might want to replace the OR function "||" by an ELSE functoin ","

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]}]

You also can do the following:

" Map[ 5-#&, gsub] " which is the same as " 5-#&/@gsub "

Wjr

--
}.
`\
I only exist in interaction

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