3
$\begingroup$

I've got the following:

list = {{{x11, y11}, {x12, y12}}, {{x21, y21}, {x22, y22}}}

The first element is a list of x/y data points. I want to divide each element of this list by the first element in the list. Then I want to do the same for the second list (and etc., for longer lists). This works:

Table[#/First[list[[i]]] & /@ list[[i]], {i, Length[list]}]

Shouldn't I be able to use Thread or MapThread or something, in order to clean this up? (So I don't have to refer to list three times?)

I'm not smart enough to do it. Thanks for any help.

$\endgroup$
4
$\begingroup$

ReplacePart can specify the operation directly:

ReplacePart[list, {i_, j_} :> list[[i, j]] / list[[i, 1]]]

result

The MapIndexed equivalent is a little shorter, but perhaps also a little less readable:

MapIndexed[# / list[[#2[[1]], 1]] &, list, {2}]
$\endgroup$
4
$\begingroup$

I think this is cleaner:

 twoListsOfPairs = {{{x11, y11}, {x12, y12}, {x13, y13}}, {{x21, y21}, {x22, y22}, {x23, y23}}};

 normalizeToFirst[list_] := Transpose[Transpose[list]/First[list]]

 normalizeToFirst /@ twoListsOfPairs

which returns:

 {{{1, 1}, {x12/x11, y12/y11}, {x13/x11, y13/y11}}, {{1, 1}, {x22/x21, y22/y21}, {x23/x21, y23/y21}}}
$\endgroup$
3
$\begingroup$

Instead of threading, I believe you can use patterns which is much cleaner

Replace[list, {i_, j_} -> {{1, 1}, j/i}, 1]

$\endgroup$
  • $\begingroup$ This is awesome, but I'm not as familiar with patterns. $\endgroup$ – rhomboidRhipper Mar 27 '18 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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