5
$\begingroup$

I have the following list of values.

{{140, 20}, {140, 15}, {175, 60}, {160, 40}, {175, 60}, {125, 20},
{120, 20}, {105, 5}, {15, 85}, {90, 5}, {150, 35}, {175, 75}, {85,
25}}

I want to say that if the first value of the pair is greater than x then add to x to the second value of the pair. Then repeat for the remaining pairs.

$\endgroup$
0

5 Answers 5

10
$\begingroup$

If

lst = {{140, 20}, {140, 15}, {175, 60}, {160, 40}, {175, 60}, {125, 20},
       {120, 20}, {105, 5}, {15, 85}, {90, 5}, {150, 35}, {175, 75}, {85, 25}};

then

With[{x = 100}, If[#1 > x, {#1, #2 + x}, {#1, #2}] & @@@ lst]
With[{x = 100}, lst /. {a_ /; a > x, y_} :> {a, x + y}]

both yield

(* {{140, 120}, {140, 115}, {175, 160}, {160, 140}, {175, 160}, {125, 120},
    {120, 120}, {105, 105}, {15, 85}, {90, 5}, {150, 135}, {175, 175}, {85, 25}} *)
$\endgroup$
13
$\begingroup$

With in-place modification. Make a copy of your data and operate on that if desired.

dat = {{140, 20}, {140, 15}, {175, 60}, {160, 40}, {175, 60}, {125, 20}, {120, 
   20}, {105, 5}, {15, 85}, {90, 5}, {150, 35}, {175, 75}, {85, 25}};

x = 100;

dat[[All, 2]] += x * UnitStep[dat[[All, 1]] - x];

dat
{{140, 120}, {140, 115}, {175, 160}, {160, 140}, {175, 160}, {125, 120},
 {120, 120}, {105, 105}, {15, 85}, {90, 5}, {150, 135}, {175, 175}, {85, 25}}

Or a one-line function without in-place modificaton:

{#, #2 + x*UnitStep[# - x]}\[Transpose] & @@ (dat\[Transpose])

Both methods above use vectorization for efficiency. If brevity is more important you can drop the Transpose steps:

{#, #2 + x*UnitStep[# - x]} & @@@ dat

Or just use If as already shown by march and anderstood, but here slightly more tersely:

{#, #2 + If[# > x, x, 0]} & @@@ dat
$\endgroup$
8
$\begingroup$

Define a function for one list:

f[{a_,b_},x_]:=If[a>x, {a,b+x}, {a,b}]

Then apply f to your list:

f[#,x]&/@ {{140, 20}, {140, 15}, {175, 60}, {160, 40}, {175, 60}, {125, 20},
{120, 20}, {105, 5}, {15, 85}, {90, 5}, {150, 35}, {175, 75}, {85,
25}}
(* with x=100: {{140, 120}, {140, 115}, {175, 160}, {160, 140}, {175, 160}, {125, 
 120}, {120, 120}, {105, 105}, {15, 85}, {90, 5}, {150, 135}, {175, 
175}, {85, 25}} *)

Possibly cleaner application with a SubValues definition:

f[x_][{a_,b_}] := If[a>x, {a,b+x}, {a,b}]

f[100] /@ list
$\endgroup$
1
  • $\begingroup$ I added a method to your answer. If you do not like it just revert the edit to remove it. $\endgroup$
    – Mr.Wizard
    Jan 19, 2017 at 17:37
6
$\begingroup$

with x = 100

Transpose[{#[[All, 1]], Clip[#[[All, 1]] , {100, 100}, {0, 100}] + #[[All, 2]]}] &@lst

{{140, 120}, {140, 115}, {175, 160}, {160, 140}, {175, 160}, {125, 120}, {120, 120}, {105, 105}, {15, 85}, {90, 5}, {150, 135}, {175, 175}, {85, 25}}

$\endgroup$
1
$\begingroup$

Using Position and MapAt:

x = 100;
alist = {{140, 20}, {140, 15}, {175, 60}, {160, 40}, {175, 60}, {125, 
   20}, {120, 20}, {105, 5}, {15, 85}, {90, 5}, {150, 35}, {175, 
   75}, {85, 25}}
pos = Position[alist[[All, 1]], _?(# > x &)]
MapAt[# + {0, 100} &, alist, pos]

{{140, 120}, {140, 115}, {175, 160}, {160, 140}, {175, 160}, {125,
120}, {120, 120}, {105, 105}, {15, 85}, {90, 5}, {150, 135}, {175,
175}, {85, 25}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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