2
$\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$ 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
    Aug 27, 2015 at 11:31

2 Answers 2

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$
4
$\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
    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
    Aug 27, 2015 at 23:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.