1
$\begingroup$

Good evening, I have a *.dat file with four columns of data, $ \{t, x_1, x, y\} $. I would like to create another *.dat file containing all the $ y $ corresponding to $ x = x_1 $.

Could you please give me suggestions on how to select only those $ y $?

Thank you in advance!

$\endgroup$
  • 2
    $\begingroup$ Select[dat, #[[2]] == #[[3]]&] would select all rows which have x1 == x. $\endgroup$ – MassDefect Jan 2 at 22:26
  • $\begingroup$ awk would be better suited than Mathematica for this task. $\endgroup$ – Roman Jan 3 at 11:11
  • $\begingroup$ There is no standard format called "dat file". How to read/write in your format depends on what the format actually is. This should be described precisely and unambiguously in the question. $\endgroup$ – Szabolcs Jan 4 at 12:01
4
$\begingroup$

As MassDefect suggested in comments, you can use Select to filter out the rows that do not satisfy your condition:

SeedRandom[1234]
dat = RandomInteger[100, {3000000, 4}];

Select[dat, #[[2]] == #[[3]] &]

However, Cases will be faster, if that's a concern:

Cases[dat, {_, x_, x_, _}]

You can compare timings and confirm that the results are the same with the two approaches:

RepeatedTiming[select = Select[dat, #[[2]] == #[[3]] &];]   (* Out: {3.3, Null} *)
RepeatedTiming[cases = Cases[dat, {_, x_, x_, _}];]         (* Out: {1.3, Null} *)
select === cases                                            (* Out: True        *)

Furthermore, if you really only want a list of the $y$ values for which the condition is true, that's very easy to obtain with Cases with almost no time penalty:

Cases[dat, {_, x_, x_, y_} :> y]

You can then export either result using e.g. Export["filename.dat", yourResult, "Table"]; look up Export and the "Table" export format in the docs.

$\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.