3
$\begingroup$

I am trying to perform a calculation on a list containing sublists. More exact: The x-axis values are time data, progressing from zero, the y-axis data are data points, which I want to normalize. I want the y data to have their minimum at zero. Therefore, I look for the smallest y value in each sublist and add or subtract (depending if it is positive or negative) from all other y values of that sublist. I want to perform the calculation for all sublists. I think I haven't quite understood what the Slot (#) exactly does?!

I post here just an example list illustrating my problem, since the real list is very long.

My approach:

list={{{0,-0.1},{1,0.5},{2,0.7},{3,1.2},{4,0.6},{5,0.5},{6,1.3}},{{0,0.1},{1,0.5},{2,0.3},{3,0.7},{4,0.8},{5,1.1},{6,1.2}}}

list//{#[[1]],(#[[2]]-Min[#[[All,2]]])}&/@#&

This approach gives the following errors:

Part::partw: Part 1 of {} does not exist. >>

Part::partw: Part 2 of {} does not exist. >>

Part::partw: Part 1 of {} does not exist. >>

General::stop: Further output of Part::partw will be suppressed during this calculation. >>

$\endgroup$
2
  • $\begingroup$ You might be interested in Rescale[]. $\endgroup$ Commented Aug 27, 2015 at 11:27
  • $\begingroup$ Cool! I didn't know that command. But just out of interest, is there a way to solve my problem with something similar I tried? $\endgroup$
    – Niki
    Commented Aug 27, 2015 at 11:31

4 Answers 4

3
$\begingroup$

First of all, your code does not return an error on my machine.

Second, using "something similar to what you tried", you might want to do

Transpose[{#[[All, 1]], #[[All, 2]] - Min[#[[All, 2]]]}] & /@ list
$\endgroup$
5
$\begingroup$

Just based on your text:

f[u_] := Module[{a, b}, {a, b} = Transpose@u; 
  Transpose[{a, b - Min@b}]]

f/@list yields:

{{{0, 0.}, {1, 0.6}, {2, 0.8}, {3, 1.3}, {4, 0.7}, {5, 0.6}, {6, 1.4}}, {{0, 0.}, {1, 0.4}, {2, 0.2}, {3, 0.6}, {4, 0.7}, {5, 1.}, {6, 1.1}}}

$\endgroup$
2
  • $\begingroup$ Puh, thanks a lot for the answer, it's very elegant. I have to wrap my head round it! I'm not very familiar with such constructs. $\endgroup$
    – Niki
    Commented Aug 27, 2015 at 12:25
  • $\begingroup$ @Niki just play around...essentially transposing rectangular array allows you deal with columns as you like then re: transposing restores initial configuration with transformed rows...play and enjoy:) $\endgroup$
    – ubpdqn
    Commented Aug 27, 2015 at 23:50
1
$\begingroup$
  list =
      {{{0, -0.1}, {1, 0.5}, {2, 0.7}, {3, 1.2}, {4, 0.6}, {5, 0.5}, {6, 1.3}}, 
        {{0, 0.1}, {1, 0.5}, {2, 0.3}, {3, 0.7}, {4, 0.8}, {5, 1.1}, {6, 1.2}}};

Using Threaded (new in 13.1)

# - Threaded[{0, Min @ #[[All, 2]]}] & /@ list

returns

{{{0, 0.}, {1, 0.6}, {2, 0.8}, {3, 1.3}, {4, 0.7}, {5, 0.6}, {6, 1.4}}, 
 {{0, 0.}, {1, 0.4}, {2, 0.2}, {3, 0.6}, {4, 0.7}, {5, 1.0}, {6, 1.1}}}
$\endgroup$
1
$\begingroup$

Using Outer and Transpose:

list =
  {{{0, -0.1}, {1, 0.5}, {2, 0.7}, {3, 1.2}, {4, 0.6}, {5, 0.5}, {6, 1.3}}, 
    {{0, 0.1}, {1, 0.5}, {2, 0.3}, {3, 0.7}, {4, 0.8}, {5, 1.1}, {6, 1.2}}};

Transpose/@Transpose@{#[[All, All, 1]], #[[All, All, 2]]
- Outer[Min, #[[All, All, 2]], 1]} &@list

{{{0, 0.}, {1, 0.6}, {2, 0.8}, {3, 1.3}, {4, 0.7}, {5, 0.6}, {6, 1.4}}, 
 {{0, 0.}, {1, 0.4}, {2, 0.2}, {3, 0.6}, {4, 0.7}, {5, 1.0}, {6, 1.1}}}
$\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.