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!

  • 2
    $\begingroup$ Select[dat, #[[2]] == #[[3]]&] would select all rows which have x1 == x. $\endgroup$
    – MassDefect
    Jan 2, 2020 at 22:26
  • $\begingroup$ awk would be better suited than Mathematica for this task. $\endgroup$
    – Roman
    Jan 3, 2020 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, 2020 at 12:01

1 Answer 1


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

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.


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.