7
$\begingroup$

goodmornig, I have a list of numbers like this:

data= {{0.00164, 60.36666}, {0.00328, 61.63334}, {0.00492, 
53.93333}, {0.00656, 42.56667}, {0.0082, 38.7}, {0.00984, 
34.43333}, {0.01148, 50.43333}, {0.01312, 
37.13334}, {0.01476, 32.96667}, {0.0164, 47.3}, {0.01804, 
42.16667}, {0.01968, 30.26667}, {0.02132, 
48.23333}, {0.02296, 40.76667}, {0.0246, 35.26667}, {0.02624,
 40.33333}, {0.02788, 41.3}, {0.02952, 34.73333}, {0.03116, 
39.56667}, {0.0328, 35.7}, {0.03444, 36.5}, {0.03608, 
34.7}, {0.03772, 36.76667}, {0.03936, 33.6}, {0.041, 
39.16667}, {0.04264, 33.43333}, {0.04428, 
34.66667}, {0.04592, 34.6}, {0.04756, 35.83333}, {0.0492, 
31.9}, {0.05084, 36.2}, {0.05248, 31.2}, {0.05412, 
32.46667}, {0.05576, 31.56667}, {0.0574, 33.53333}, {0.05904,
 28.83333}, {0.06068, 32.46667}, {0.06232, 28.}, {0.06396, 
30.4}, {0.0656, 28.66667}, {0.06724, 31.46667}, {0.06888, 
28.8}, {0.07052, 31.83333}, {0.07216, 27.43333}, {0.0738, 
29.1}, {0.07544, 28.73333}, {0.07708, 29.83333}, {0.07872, 
26.13333}, {0.08036, 28.66667}, {0.082, 25.46667}, {0.08364, 
27.03333}, {0.08528, 25.16667}, {0.08692, 
25.96667}, {0.08856, 22.8}, {0.0902, 26.03333}, {0.09184, 
23.6}, {0.09348, 25.}, {0.09512, 23.2}, {0.09676, 
24.1}, {0.0984, 21.26667}, {0.10004, 25.36667}, {0.10168, 
22.03333}, {0.10332, 24.2}, {0.10496, 20.86667}, {0.1066, 
23.96667}, {0.10824, 20.1}, {0.10988, 21.03333}, {0.11152, 
19.6}, {0.11316, 22.26667}, {0.1148, 20.}, {0.11644, 
22.4}, {0.11808, 19.66667}, {0.11972, 20.3}, {0.12136, 
15.53333}, {0.123, 19.83333}, {0.12464, 18.66667}, {0.12628, 
19.63333}, {0.12792, 18.1}, {0.12956, 21.06667}, {0.1312, 
17.9}, {0.13284, 18.03333}, {0.13448, 15.13333}, {0.13612, 
19.93333}, {0.13776, 15.36667}, {0.1394, 16.73333}, {0.14104,
 14.83333}, {0.14268, 17.33333}, {0.14432, 
15.43333}, {0.14596, 17.26667}, {0.1476, 14.36667}, {0.14924,
 18.16667}, {0.15088, 14.7}, {0.15252, 15.6}, {0.15416, 
13.23333}, {0.1558, 16.}, {0.15744, 13.23333}, {0.15908, 
16.3}, {0.16072, 13.46667}, {0.16236, 14.56667}, {0.164, 
12.26667}, {0.16564, 14.36667}, {0.16728, 
12.43333}, {0.16892, 13.96667}, {0.17056, 12.43333}, {0.1722,
 14.6}, {0.17384, 11.}, {0.17548, 14.26667}, {0.17712, 
11.73333}, {0.17876, 12.6}, {0.1804, 10.}, {0.18204, 
13.5}, {0.18368, 10.63333}, {0.18532, 12.13333}, {0.18696, 
10.43333}, {0.1886, 10.8}, {0.19024, 8.733334}, {0.19188, 
11.66667}, {0.19352, 10.53333}, {0.19516, 12.56667}, {0.1968,
 10.26667}, {0.19844, 10.8}, {0.20008, 7.8}, {0.20172, 
10.}, {0.20336, 9.533334}, {0.205, 8.233334}, {0.20664, 
7.}, {0.20828, 8.9}, {0.20992, 7.933333}, {0.21156, 
9.066667}, {0.2132, 6.4}, {0.21484, 8.266666}, {0.21648, 
6.466667}, {0.21812, 8.166667}, {0.21976, 6.}, {0.2214, 
9.566667}, {0.22304, 7.1}, {0.22468, 9.566667}, {0.22632, 
6.566667}, {0.22796, 8.833333}, {0.2296, 6.433333}, {0.23124,
 7.6}, {0.23288, 5.433333}, {0.23452, 9.5}, {0.23616, 
5.233333}, {0.2378, 6.866667}, {0.23944, 6.2}, {0.24108, 
8.9}, {0.24272, 6.266667}, {0.24436, 7.333333}, {0.246, 
4.6}, {0.24764, 6.833333}, {0.24928, 4.566667}, {0.25092, 
6.266667}, {0.25256, 3.5}, {0.2542, 5.966667}, {0.25584, 
4.9}, {0.25748, 4.866667}, {0.25912, 2.533333}, {0.26076, 
5.766667}, {0.2624, 5.333333}, {0.26404, 5.566667}, {0.26568,
 4.633333}, {0.26732, 6.1}, {0.26896, 1.6}, {0.2706, 
5.4}, {0.27224, 5.233333}, {0.27388, 7.533333}, {0.27552, 
4.666667}, {0.27716, 6.466667}, {0.2788, 4.166667}, {0.28044,
 6.766667}, {0.28208, 4.066667}, {0.28372, 5.7}, {0.28536, 
2.4}, {0.287, 4.4}, {0.28864, 2.533333}, {0.29028, 
2.566667}, {0.29192, 1.3}, {0.29356, 5.866667}, {0.2952, 
4.7}, {0.29684, 4.533333}, {0.29848, 1.933333}, {0.30012, 
5.333333}, {0.30176, 3.333333}, {0.3034, 5.366667}, {0.30504,
 2.233333}, {0.30668, 2.766667}, {0.30832, 
0.8666667}, {0.30996, 3.733333}, {0.3116, 
2.566667}, {0.31324, 5.033333}, {0.31488, 
1.966667}, {0.31652, 1.633333}, {0.31816, 
0.6333333}, {0.3198, 3.833333}, {0.32144, 
1.933333}, {0.32308, 
2.633333}, {0.32472, -0.2333333}, {0.32636, 
3.}, {0.328, -0.1}, {0.32964, 
2.966667}, {0.33128, -0.7}, {0.33292, 2.5}, {0.33456, 
0.7}, {0.3362, 2.5}, {0.33784, -0.6}, {0.33948, 
2.833333}, {0.34112, 1.366667}, {0.34276, 2.533333}, {0.3444,
 0.1666667}, {0.34604, 3.4}, {0.34768, 0.4}, {0.34932, 
3.166667}, {0.35096, 0.7666667}, {0.3526, 
1.933333}, {0.35424, -0.7333333}, {0.35588, 
2.266667}, {0.35752, -0.6}, {0.35916, 3.1}, {0.3608, 
0.5}, {0.36244, 
3.}, {0.36408, -1.233333}, {0.36572, -0.03333334}, {0.36736, \
-0.5}, {0.369, 2.666667}, {0.37064, -0.5333334}, {0.37228, 
2.366667}, {0.37392, 1.633333}, {0.37556, 2.7}, {0.3772, 
0.3333333}, {0.37884, 0.1}, {0.38048, -0.8333333}, {0.38212, 
0.7666667}, {0.38376, -0.1333333}, {0.3854, 
0.8}, {0.38704, -0.6}, {0.38868, 
1.766667}, {0.39032, -0.2333333}, {0.39196, 
2.1}, {0.3936, -0.5}, {0.39524, 
1.133333}, {0.39688, -1.6}, {0.39852, 0.9666666}, {0.40016, 
0.03333334}, {0.4018, 
0.03333334}, {0.40344, -0.3333333}, {0.40508, 
1.366667}, {0.40672, -1.4}, {0.40836, 
0.9}, {0.41, -0.1}, {0.41164, 
0.1333333}, {0.41328, -2.666667}, {0.41492, 
0.6}, {0.41656, -0.2}, {0.4182, 
0.6}, {0.41984, -3.233333}, {0.42148, -1.566667}, {0.42312, \
-0.5666667}, {0.42476, 
1.533333}, {0.4264, -1.833333}, {0.42804, -0.3333333}, \
{0.42968, -1.333333}, {0.43132, 
0.6666667}, {0.43296, -1.533333}, {0.4346, -0.3}, {0.43624, \
-1.833333}, {0.43788, 
0.1666667}, {0.43952, -0.4666667}, {0.44116, 
0.9333333}, {0.4428, -0.9333333}, {0.44444, -0.5666667}, \
{0.44608, -3.}, {0.44772, 
0.5666667}, {0.44936, -0.8333333}, {0.451, 
1.966667}, {0.45264, 0.4}, {0.45428, 
1.766667}, {0.45592, -0.2333333}, {0.45756, 
1.}, {0.4592, -0.5666667}, {0.46084, 
1.233333}, {0.46248, -2.033333}, {0.46412, -2.833333}, \
{0.46576, -3.233333}, {0.4674, -0.2333333}, {0.46904, \
-2.533333}, {0.47068, -1.066667}, {0.47232, -1.333333}, \
{0.47396, 
0.4666667}, {0.4756, -2.466667}, {0.47724, -0.3666667}, \
{0.47888, -1.1}, {0.48052, 
1.366667}, {0.48216, -1.466667}, {0.4838, -2.033333}, \
{0.48544, -2.233333}, {0.48708, -0.3666667}, {0.48872, \
-1.433333}, {0.49036, 1.1}, {0.492, -0.5333334}, {0.49364, 
0.2333333}, {0.49528, -2.233333}, {0.49692, -1.033333}, \
{0.49856, -0.8666667}, {0.5002, 
0.9666666}, {0.50184, -2.333333}, {0.50348, 
0.4333333}, {0.50512, -1.433333}, {0.50676, -3.066667}, \
{0.5084, -3.333333}, {0.51004, 
0.06666667}, {0.51168, -0.3666667}, {0.51332, 
1.166667}, {0.51496, -0.8}, {0.5166, 
0.8666667}, {0.51824, -2.2}, {0.51988, -1.766667}, {0.52152, \
-3.666667}, {0.52316, 0.2}, {0.5248, -0.06666667}, {0.52644, 
0.}, {0.52808, -1.3}, {0.52972, -0.5333334}, {0.53136, \
-1.4}, {0.533, -0.2}, {0.53464, -2.4}, {0.53628, -1.133333}, \
{0.53792, -2.8}, {0.53956, -1.4}, {0.5412, -2.033333}, \
{0.54284, -1.166667}, {0.54448, -2.166667}, {0.54612, -1.}, \
{0.54776, -2.466667}, {0.5494, -0.9666666}, {0.55104, -1.6}, \
{0.55268, -1.5}, {0.55432, -2.5}, {0.55596, -1.4}, {0.5576, \
-1.366667}, {0.55924, -0.1333333}, {0.56088, -2.633333}, \
{0.56252, -0.2666667}, {0.56416, -0.7666667}, {0.5658, \
-1.033333}, {0.56744, -3.366667}, {0.56908, -1.366667}, \
{0.57072, -2.5}, {0.57236, -0.2333333}, {0.574, -3.033333}, \
{0.57564, -1.466667}, {0.57728, -1.566667}, {0.57892, -0.1}, \
{0.58056, -2.033333}, {0.5822, -0.8}, {0.58384, -1.833333}, \
{0.58548, -0.9666666}, {0.58712, -2.9}, {0.58876, -0.9333333}, \
{0.5904, -1.766667}, {0.59204, -0.9333333}, {0.59368, \
-1.666667}, {0.59532, -0.2666667}, {0.59696, -2.4}, {0.5986, \
-2.6}, {0.60024, -2.466667}, {0.60188, 
0.06666667}, {0.60352, -1.966667}, {0.60516, 
0.1666667}, {0.6068, -2.5}, {0.60844, -1.466667}, {0.61008, \
-1.833333}, {0.61172, -0.8333333}, {0.61336, -2.1}, {0.615, \
-2.066667}, {0.61664, -2.266667}, {0.61828, -2.6}, {0.61992, \
-2.3}, {0.62156, -1.766667}, {0.6232, -3.233333}, {0.62484, \
-0.5}, {0.62648, -2.433333}, {0.62812, -2.433333}, {0.62976, \
-3.833333}, {0.6314, -1.033333}, {0.63304, -2.166667}, {0.63468,
 0.8333333}, {0.63632, -1.9}, {0.63796, 
0.2}, {0.6396, -1.9}, {0.64124, -1.5}, {0.64288, \
-2.733333}, {0.64452, -0.3333333}, {0.64616, -0.9333333}, \
{0.6478, -0.06666667}, {0.64944, -3.433333}, {0.65108, -2.7}, \
{0.65272, -2.166667}}

I want to find the min value of the second elements (in this case it seem to be -3.43333) and then shift all second elements by that value (in this case +3.433333) creating a new list. Can you help me?

$\endgroup$
1
  • $\begingroup$ Use indexing to grab all the elements of the second column : Min[data[[All, 2]]] (output: -3.83333) and use MapAt to shift every single element: MapAt[# + 3.833333 &, data, {All, 2}] $\endgroup$
    – Ben Izd
    Commented Nov 8, 2021 at 13:34

6 Answers 6

8
$\begingroup$

There are many ways to do this, here is one taking advantage of SubsetMap

SubsetMap[#-Min[#]&,data, {All,2}]

Or alternatively for speed (Using Block, Transpose and Part)

Block[
 {
   x=data[[All, 1]],
   y=data[[All, 2]],
   min
 },
 min = Min[y];
 Transpose[{x,y-min}]
]

Or avoiding Part...

 Block[
   {x,y},
   {x,y}=Transpose@data;
   Transpose[{x,y-Min[y]}]
 ]
$\endgroup$
5
$\begingroup$

Comparing the timings

RepeatedTiming[data2 = SubsetMap[# - Min[#] &, data, {All, 2}];] (* rhermans *)

(* {0.00151529, Null} *)

RepeatedTiming[min = Min[Transpose[data][[2]]]; 
 data3 = data /. {x_, y_} -> {x, y - min};] (* Alexei Boubitch *)

(* {0.000237703, Null} *)

RepeatedTiming[
 data4 = (min = Min[data[[All, 2]]]; (# - {0, min}) & /@ data);]

(* {0.000131082, Null} *)

EDIT:

RepeatedTiming[
 data5 = Block[{x = data[[All, 1]], y = data[[All, 2]], min}, min = Min[y];
    Transpose[{x, y - min}]];] (* rhermans *)

(* {0.0000810067, Null} *)

Verifying equivalence of results,

data2 === data3 === data4 === data5

(* True *)
$\endgroup$
4
  • $\begingroup$ Any insights that may explain to some extent the time differences? $\endgroup$
    – rhermans
    Commented Nov 8, 2021 at 15:36
  • 2
    $\begingroup$ @rhermans - My assumptions: SubsetMap is designed to handle a broad range of inputs and carries more overhead to provide that flexibility. The other approaches are highly tailored to this specific problem. Rule-based replacements add pattern matching tests in addition to the basic operation being performed. However, if the problem is a one-time deal as opposed to being used repeatedly, the timing is of far less importance than the user's ease of programming, i.e., whatever readily comes to mind is best. $\endgroup$
    – Bob Hanlon
    Commented Nov 8, 2021 at 15:52
  • $\begingroup$ It seems that avoiding defining min using With or Block speeds things a bit too, see my edit. $\endgroup$
    – rhermans
    Commented Nov 8, 2021 at 16:18
  • 1
    $\begingroup$ data[[All,2]] -= Min[data[[All,2]]] is even faster, for small data sets such as the one above. For larger datasets (I did a RandomReal with 1 million pairs) it seems that rhermans method is winning by a bit. Nevertheless, if one wants to keep this kind of calculations fast, one should have an individual list for each column instead of combining them into a table. $\endgroup$
    – a20
    Commented Nov 8, 2021 at 17:40
4
$\begingroup$

Try the following. The next statement yields the minimal second value:

min = Min[Transpose[data][[2]]]

(*  -3.83333  *)

and this makes the shift:

data2 = data /. {x_, y_} -> {x, y - min};

In the new data the minimum second value will be zero:

min2 = Min[Transpose[data2][[2]]]

(*  0.  *)

Let us plot them:

ListPlot[{data, data2}, PlotStyle -> {Blue, Red}]

enter image description here

Have fun!

$\endgroup$
7
  • $\begingroup$ Why do you use transpose when you could simply do Min[data[[All,2]]] ? $\endgroup$
    – a20
    Commented Nov 8, 2021 at 13:52
  • $\begingroup$ @a20 Why not? These things are the same, are they not? $\endgroup$ Commented Nov 8, 2021 at 13:55
  • 1
    $\begingroup$ they produce the same result, but transposing the whole list first takes a lot longer time, about a factor of 4 difference on my computer. I do not know how they are implemented. $\endgroup$
    – a20
    Commented Nov 8, 2021 at 14:05
  • $\begingroup$ @a20 If you take AbsoluteTiming you will find that the former approach takes 0.00009 s and the latter 0.00006 s. This difference is not worth discussing. Of course, another story if the OP will start working on lists at least, 100 000 times greater than the one shown as data. However, it does not seem to be the case. A nice thing about Mma is that in it almost every problem can be solved in several ways, and each of us chooses the way better corresponding to his personality: I choose this and you that. $\endgroup$ Commented Nov 8, 2021 at 16:22
  • 1
    $\begingroup$ @a20 I do not take it as a criticism (though I have nothing against criticism as well). It is a long-standing discussion. Indeed, people with a programmatic background tend to search for the fastest solution. Logically, however, if one needs a really fast solution he takes another soft, like C or Fortran or another fast compiler. Typically, one uses Mma in the case when the timing is not the main problem. In many cases, the problems we try to solve with Mma are not that time-consuming. In this case, the bottleneck maybe the question "How fast it takes you to search for the solution?" $\endgroup$ Commented Nov 8, 2021 at 18:33
2
$\begingroup$

To get the minimum value min from the second column of data, here are a few variants:

MinimalBy[data, Last][[1, -1]]
RankedMin[data[[All, 2]], 1]
TakeSmallestBy[data, Last, 1][[1, -1]]
SortBy[data, Last][[1, -1]]

d3 = MapThread[{#1, #2 - min} &, Transpose@data];

ListPlot[{data, d3}, PlotStyle -> {Blue, Red}]
$\endgroup$
2
$\begingroup$
data = 
 {{0.0016, 60.3}, {0.0032, 61.6}, {0.0049, 53.9}, 
  {0.0065, 42.5}, {0.0082, 38.7}, {0.0098, 34.4}};

Using ArrayReduce (new in 12.2) and ReplaceAt (new in 13.1)

With[{m = Last @ ArrayReduce[Min, data, 1]},
 ReplaceAt[x_ :> x - m, {All, 2}] @ data]

{{0.0016, 25.9}, {0.0032, 27.2}, {0.0049, 19.5}, {0.0065, 8.1}, {0.0082, 4.3}, {0.0098, 0.}}

$\endgroup$
2
$\begingroup$
data = {{0.0016, 60.3}, {0.0032, 61.6}, {0.0049, 53.9},
       {0.0065, 42.5}, {0.0082, 38.7}, {0.0098, 34.4}};

Grabbing the @eldo's data and using ReplacePart:

With[{m = Last@First@SortBy[Last]@data}, 
ReplacePart[data, i_ :> data[[i]] - Threaded[{0, m}]]]

Result:

{{0.0016, 25.9}, {0.0032, 27.2}, {0.0049, 19.5}, {0.0065, 8.1}, {0.0082, 4.3}, {0.0098, 0.}}

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