0
$\begingroup$

I make two tables,gaussian and pedestal:

σ = 150;
x0 = 10;
x1 = 1;
x2 = 20;
ped1 = 15;
ped2 = 40; 
Gauss[x_] := E^(-(x - x0)^2/(2 σ^2));
gaussian = Table[{x, N[Gauss[x]]}, {x, x1, x2}]

{{1, 0.998202}, {2, 0.998579}, {3, 0.998912}, {4, 0.9992}, {5, 
  0.999445}, {6, 0.999645}, {7, 0.9998}, {8, 0.999911}, {9, 
  0.999978}, {10, 1.}, {11, 0.999978}, {12, 0.999911}, {13, 
  0.9998}, {14, 0.999645}, {15, 0.999445}, {16, 0.9992}, {17, 
  0.998912}, {18, 0.998579}, {19, 0.998202}, {20, 0.99778}}

pedestal = Table[{y, 1}, {y, ped1, ped2}]

{{15, 1}, {16, 1}, {17, 1}, {18, 1}, {19, 1}, {20, 1}, {21, 1}, {22, 
  1}, {23, 1}, {24, 1}, {25, 1}, {26, 1}, {27, 1}, {28, 1}, {29, 
  1}, {30, 1}, {31, 1}, {32, 1}, {33, 1}, {34, 1}, {35, 1}, {36, 
  1}, {37, 1}, {38, 1}, {39, 1}, {40, 1}}

I want to add gaussian and pedestal such that only the second value in each pair is added, but the x-axis stays the same. So as to result basically in a Gaussian with a uniform pedestal.

Is there a more intuitive way of doing this, without having to read out the first and second values of each list, defining the x-axis of the final plot, etc?

Can I just add the two lists with some option to only add the second entry of each tuple?

$\endgroup$
  • $\begingroup$ you want to add gaussian to pedestal or the other way around? They have different x values, but I guess you want to add them position by position? You could try gaussian[[All, 2]] = gaussian[[All, 2]] + pedestal[[All,2]] (or you could use a += but I tend not to) $\endgroup$ – Jason B. Jan 12 '17 at 21:18
2
$\begingroup$

maybe this is what you mean?

allx = gaussian[[All, 1]]~Join~pedestal[[All, 1]] // Union
Table[ { i, 
  Total@Select[Join[gaussian, pedestal], #[[1]] == i &][[All, 
     2]] }, {i, allx}] 

or

{#[[1, 1]], Total@#[[All, 2]]} & /@ 
 GatherBy[Join[gaussian, pedestal], #[[1]] &]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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