Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am making a larger program, so I would prefer to give a short example of my problem:

    soba={2.45649, 4.04015, 4.92679, 4.03324, 0.761532, 2.41486, 1.96081,
4.37201, 4.26696, 2.99139}

While[NumberQ[Select[soba, Function[{a}, a > 2], 1][[1]]], 
 soba = (soba /. Select[soba, Function[{a}, a > 2], 1][[1]] -> 1)]


Wanted output: 
    {1, 1, 1, 1, 0.761532, 1, 1.96081, 1, 1, 1}

The reason why I want this to happen is: First I want to find the first value in the list that matches criteria, second I want a rule to be applied in this case it is just changing the value to 1. But in the real code it is changing several values based on some other rules, it could for example change 2.45649, 4.04015, 4.92679, 4.03324 with 1. Then I want it to run over the list again, but since it now has changed all this values 2.45649, 4.04015, 4.92679, 4.03324to 1 my next value it would pick is 2.41486.Then change the list based on some rules and run the code all over again on the updated list.

First off all you can see that the code does not work, second THERE HAS TO BE A BETTER WAY!

share|improve this question
    
Yes because the of the other rules. –  ALEXANDER Apr 1 at 19:01

1 Answer 1

up vote 2 down vote accepted

A construct like this will do what you want. I followed your basic example of changing the first candidate found, then rinse-and-repeat:

soba = {2.45649, 4.04015, 4.92679, 4.03324, 0.761532, 2.41486, 
        1.96081, 4.37201, 4.26696, 2.99139}

FixedPointList[ ReplacePart[#, Position[#, x_ /; x > 2, 1, 1] -> 1] &, soba]

(* {{2.45649, 4.04015, 4.92679, 4.03324, 0.761532, 2.41486, 1.96081, 
  4.37201, 4.26696, 2.99139}, {1, 4.04015, 4.92679, 4.03324, 0.761532,
   2.41486, 1.96081, 4.37201, 4.26696, 2.99139}, {1, 1, 4.92679, 
  4.03324, 0.761532, 2.41486, 1.96081, 4.37201, 4.26696, 2.99139}, {1,
   1, 1, 4.03324, 0.761532, 2.41486, 1.96081, 4.37201, 4.26696, 
  2.99139}, {1, 1, 1, 1, 0.761532, 2.41486, 1.96081, 4.37201, 4.26696,
   2.99139}, {1, 1, 1, 1, 0.761532, 1, 1.96081, 4.37201, 4.26696, 
  2.99139}, {1, 1, 1, 1, 0.761532, 1, 1.96081, 1, 4.26696, 
  2.99139}, {1, 1, 1, 1, 0.761532, 1, 1.96081, 1, 1, 2.99139}, {1, 1, 
  1, 1, 0.761532, 1, 1.96081, 1, 1, 1}, {1, 1, 1, 1, 0.761532, 1, 
  1.96081, 1, 1, 1}}
*)

The output is a list of the iterations of changes until no mare changes can be made. If you're only interested in the end result, just use FixedPoint instead of FixedPointList.

Obviously, the function used within can be arbitrarily complex.

share|improve this answer
    
Brilliant! I tried looking for something like fixedPoint, but some how I managed to miss it! Thank you so much! Made my day! MOHAHAHA –  ALEXANDER Apr 1 at 21:01
1  
@ALEXANDER: Glad it is useful. If you want to control the number of applications, the above would become NestList[ReplacePart[#, Position[#, x_ /; x > 2, 1, 1] -> 1] &, soba, 2], where the 2 in the last argument is the number of times to apply your function/rules. –  rasher Apr 2 at 1:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.