I have a bunch of datasets which look like:
3 7 1 6 5
8 2 4 1 2
8 1 5 2 5
...
and I need to make a list of pairs such that the first element makes a pair with other elements in the same line.
3 7
3 1
3 6
...
I usually use Table
because it is fast enough for $10^3$ elements, but as my dataset gets bigger, it's no longer efficient. For example:
data = RandomInteger[{1, 10}, {10^7, 5}];
Flatten[Table[{data[[i, 1]], data[[i, j]]}, {i, 1, Length@data}, {j,
2, 5}], 1]//AbsoluteTiming//First
(* 39.828744 *)
What is the most efficient way to find this pair list?
Timing
Finally I had a chance to do a timing on my real machine:
(* Kuba *)
0.726869
(* ciao *)
1.297926
(* nested table*)
2.146111
(* Shutao Tang *)
11.290944
(* Mr.Wizard♦ *)
57.232387
Although it seems results can be (completely) different if I do a timing on my old laptop!
Integer
? Or always a machine-precisionReal
? Or mixture of different types of data? $\endgroup$Integer
. $\endgroup$