1
$\begingroup$

Say that I have a list of the form

A={1,2,{{3,4},{5,6}}}

I want to manipulate the first elements of the last sublist. For example, in the case of multiplication by two, the resulting matrix should become

B={1,2,{{6,4},{10,6}}}

In my case, the third sublist contains around 40,000 pairs of numbers. If possible, I would like to avoid iterating through each data point. Any help would be greatly appreciated.

$\endgroup$
  • 2
    $\begingroup$ B = A; B[[-1, All, 1]] *= 2;, related: 42455 $\endgroup$ – Kuba Jun 15 at 12:54
  • $\begingroup$ Have you looked at SubsetMap? $\endgroup$ – Carl Lange Jun 15 at 13:06
3
$\begingroup$

You can use Map. Suppose f is the function you want to apply to the latter part of the list:

a = {1, 2, {{3, 4}, {5, 6}}};
Map[f, a, {2}]
{1, 2, {f[{3, 4}], f[{5, 6}]}}

For your particular f (multiplication by 2)

f[{x_, y_}] := {2 x, y};
Map[f, a, {2}]
{1, 2, {{6, 4}, {10, 6}}}
$\endgroup$
  • 1
    $\begingroup$ Tweak that definition of f slightly to match his example output $\endgroup$ – Bill Jun 15 at 20:06
  • $\begingroup$ @Bill -- It's now tweaked... $\endgroup$ – bill s Jun 16 at 4:46

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.