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 ]
Hein Hundal

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

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
> one. Help please?!
> Thanks
> Andre Bindon
> Ablerta Hospital Edmonton


The function you have built works well for values of 3 and 4, but for a value of 1 (or 2),
the clause If[gsub[[n]]==1,gsub[[n]]=4] converts the 1 into a 4 and returns the value 4. If
this clause returned True then the program would go on to the next value of n, but because 4
is returned, the remaining If clauses are evaluated. So, If[gsub[[n]]==4,gsub[[n]]=1]
converts the 4 back into a 1. To Fix your code, you could have the If[]'s return True when
their test is true.
Do[
If[gsub[[n]]==1,gsub[[n]]=4;True]||
If[gsub[[n]]==2,gsub[[n]]=3;True]||
If[gsub[[n]]==3,gsub[[n]]=2;True]||
If[gsub[[n]]==4,gsub[[n]]=1;True],
{n,1,Length[gsub]}]

There are many other ways to achieve the same goal. For example:

gsub /. Table[ a -> (5-a), {a,4}]


Hein Hundal







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.