I have a list with 33600 Elements and I have to replace every element bigger than 6000 with its half. I "practiced" with a smaller list and tried the following:
List1 = {1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000}; ListNew = {}; Do[ { Numb = Take[List1, n ;; n], NumbNew = 0.5 Numb, ConditionalExpression[Numb > 9000, ListNew = Append[ListNew, NumbNew]], ListNew = Append[ListNew, Numb] } , {n, 1, Length[List1]}] I want ListNew to look like this:
{1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 5000}
But what I get is this:
{5000., 1000, 5000., 2000, 5000., 3000, 5000., 4000, 5000., 5000,
5000., 6000, 5000., 7000, 5000., 8000, 5000., 9000, 5000., 10000}
I tried to use If too, but neither did it work
Is there any way to fix this?
ListNew = If[# > 6000, #/2, #] & /@ List1or long form:fn = Function[{x}, If[x > 6000, x/2, x]]; ListNew = Map[fn, List1]$\endgroup$> 9000and your expected answer looks like you've used> 9000too. As for your coding style, avoid proceduralDoloops andAppend, and instead prefer functional constructs likeMapwherever possible except where the procedural approach is absolutely necessary. $\endgroup$