0
$\begingroup$

I have two lists (first one is data, second one is background) of files that are composed of X and Y coordinates. X coordinates are the same for both lists. I would like to subtract the y coordinates of corresponding files (A1-A2, B1-B2, etc) for whole data set. Is there a quick way to do it? data1 is composed of {a1.txt, a2.txt} and data2 of{b1.txt,b2.txt}

I can only imagine doing a small for loop in which I use following code:

data1[[1]][[All, 2]] - data2[[1]][[All, 2]]

and then I MapThread X and new Y coordinate. data1[[1]]

 X  Y1
0.008362    837448.1111
0.028126    23665.24245
0.048397    2174.605716
0.068667    528.9201242
0.088938    205.8876254
0.109208    98.85484604
0.129477    65.01622775
0.149746    43.40275276
0.170015    35.06444229
0.190282    25.51391261
0.200416    21.10450766

data2[[1]]

X   Y4
0.008362    0.008166246
0.028126    0.02746757
0.048397    0.047264026
0.068667    0.067059506
0.088938    0.086855961
0.109208    0.106651441
0.129477    0.126445943
0.149746    0.146240446
0.170015    0.166034949
0.190282    0.185827498
0.200416    0.195724261

https://www.dropbox.com/sh/gh4ud83lrgfa3jo/AADFIaofouq9X9OUJmeaQ0cna?dl=0

$\endgroup$
0

1 Answer 1

1
$\begingroup$

Your initial thought sounds about right.

A relatively clean way to do this, including very simple error checking, would be something like

diff::badx = "The abscissa values in `1` don't match those in `2`.";
diff[list1_, list2_] :=
  Module[{out = list1},
    If[list1[[All, 1]] != list2[[All, 1]],
      Return[Message[diff::badx, list1, list2], Module]];
    out[[All, 2]] = out[[All, 2]] - list2[[All, 2]];
    out
  ]

In general, your use-case looks like something from spectroscopy. I deal with similar problems frequently, often with quite large datasets. Because of this I find it beneficial to always work with packed arrays, which is much easier if I keep the X and Y values separate.

In this case I would do something like

data = <||> (*initialize an association*)
{data["X"], data["Signal"]} = Transpose[Import["a1.txt"]];
data["Background"] = Last@Transpose[Import["b1.txt"]];
data["Corrected"] = data["Signal"] - data["Background"];

It's very important here to have code that makes sure, that the X values are always the same for all datasets or, otherwise, coerces the datasets to a common set of X values.

Plotting is then trivial like so:

ListPlot[Transpose[{data["X"],data["Corrected"]}]]
$\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.