0

I have generated noise with RandomVariate and am trying to add it to the y values only of a list of {x, y}-coordinates. I'm looking for a general way to add the noise (one-dimensional list) to the y-values of my list (data) such that given:

noise = {α, β, γ};
data = {{1, a}, {2, b}, {3, c}};

the result is

{{1, a + α}, {2, b + β}, {3, c + γ}}

I could do this with Table in what feels like a really inelegant process, but is there a way to do it in a simple algebraic command with only Take? For example, I know I can take the second column of data only with data[[All,2]], but the resulting list will include only the y values. I would prefer not to have to split up and recombine the lists.

Edit

To clarify, I would really like to be able to do this without defining additional variables.

Nate
  • 103
  • 5

3 Answers3

3

Here is a community wiki where we can accumulate answers.

noise = {α, β, γ};
data = {{1, a}, {2, b}, {3, c}};

J.M.

data + Transpose[PadLeft[{noise}, {2, Automatic}]]

{{1, a + α}, {2, b + β}, {3, c + γ}}

m_goldberg

MapThread[#1 + {0, #2} &, {data, noise}]

{{1, a + α}, {2, b + β}, {3, c + γ}}

Kuba

data[[;; , 2]] += noise; data

{{1, a + α}, {2, b + β}, {3, c + γ}}

march

A silly way that illustrates a clever trick I learned here:

Module[{i = 1},
  Replace[data, {a_, b_} :> {a, b + noise[[i++]]}, 2]]

BoLe

data + ({0, #} & /@ noise)
m_goldberg
  • 107,779
  • 16
  • 103
  • 257
1

Using the binary operator form of Plus,

plus[y_][x_]:=x+y

Makes it more readable than (# + noise)& in the following:

data // Transpose // MapAt[plus[noise], 2] // Transpose

{{1, a + [Alpha]}, {2, b + [Beta]}, {3, c + [Gamma]}}

alancalvitti
  • 15,143
  • 3
  • 27
  • 92
0
f = # + ToExpression[
FromCharacterCode[{1, -1, 1}.Flatten[
   ToCharacterCode /@ {SymbolName[#], "a", "\[Alpha]"}]]] &

data /. u : (a | b | c) :> f[u]
mikado
  • 16,741
  • 2
  • 20
  • 54
  • What happens if some of the values of a, b and c are the same? E.g. a = b =1; c = 2 or even all a = b = c = 3. – Michael E2 Aug 02 '16 at 20:17