2

I have a set of points:

data := {{10, 15}, {20, 45}, {30, 97}, {40, 162}, {50, 221}, {60, 249}, {70, 231}, {80, 166}, {90, 75}, {100, -5}}

What I want to do is leave the first position of each bracket as is, but multiply the second by a number, let's say 2, so I get

{{10, 30}, {20, 90}, {30, 194},...} and so on.

How can I do that?

Patrycja
  • 55
  • 6

1 Answers1

4
data = {{10, 15}, {20, 45}, {30, 97}, {40, 162}, {50, 221}, {60, 
   249}, {70, 231}, {80, 166}, {90, 75}, {100, -5}};

data /. {a_, b_} :> {a, 2 * b}

{{10, 30}, {20, 90}, {30, 194}, {40, 324}, {50, 442}, {60, 498}, {70, 462}, {80, 332}, {90, 150}, {100, -10}}

Also:

MapAt[2 # &, data, {All, 2}]

and

Query[All, {2 -> (2 # &)}] @ data

(* same result *)

If you want to modify data inline:

data[[All, 2]] *= 2;

data

{{10, 30}, {20, 90}, {30, 194}, {40, 324}, {50, 442}, {60, 498}, {70, 462}, {80, 332}, {90, 150}, {100, -10}}

Recommended link:

Elegant operations on matrix rows and columns

eldo
  • 67,911
  • 5
  • 60
  • 168