4
$\begingroup$

How can we embed a counter in the Map function?

Consider a data set such as data = {{a, b, c, d}, {e, f}, {g, h, i}}. The desired function would output

{{0, a}, {0, b}, {0, c}, {0, d}, {1, e}, {1, f}, {2, g}, {2, h}, {2, i}}

Responders are not constrained to use Map.

N.B. The answer does not appear to be in Indexed Map Thread.

$\endgroup$
4
$\begingroup$

I believe you are looking for MapIndexed, and possibly Join/Apply:

Join @@ MapIndexed[{#2[[1]] - 1, #} &, data, {2}]
{{0, a}, {0, b}, {0, c}, {0, d}, {1, e}, {1, f}, {2, g}, {2, h}, {2, i}}

If this is not what you want or you find it difficult to apply please update the question.

$\endgroup$
  • $\begingroup$ Perfect. Another great answer MrW. Thanks. $\endgroup$ – dantopa May 29 '15 at 17:47
  • $\begingroup$ @Mr. Wizard may I ask what is the difference in terms of efficiency if you map Flatten at the first level, i.e. Flatten[MapIndexed[{#2[[1]], #} &, data, {2}], 1] ? $\endgroup$ – Athanassios Aug 11 '16 at 11:59
  • 3
    $\begingroup$ @Athanassios First, although you may not have been writing technically, note that Flatten[foo, 1] is not mapping Flatten at levelspec {1}, i.e. Map[Flatten, foo, {1}], just in case that should become a point of confusion. I typically find that Join @@ is faster, but often more importantly it preserves packed arrays. Since MapIndexed does not return packed arrays the latter is not evident here. Take for example x = Range @ RandomInteger[{1, 7}, 5*^4]; then RepeatedTiming[Developer`PackedArrayQ @ Flatten[x, 1]] vs RepeatedTiming[Developer`PackedArrayQ[Join @@ x]] $\endgroup$ – Mr.Wizard Aug 11 '16 at 14:15
  • $\begingroup$ @Athanassios On my system (10.1.0 under Windows) the results are {0.0207, False} and {0.00256, True}. So not only is Join nearly an order of magnitude faster, but it returns a packed array with all the benefits that entails. Out of habit I therefore use Join @@ even when it may not be strictly superior, though again I think you will often find it faster even when working with unpacked lists. $\endgroup$ – Mr.Wizard Aug 11 '16 at 14:17
  • 1
    $\begingroup$ I have just found that there has been a lot of discussion in this community about packed vs unpacked arrays. This is one of the posts, thank you again for pointing me at the right direction to learn more mathematica.stackexchange.com/questions/3496/… $\endgroup$ – Athanassios Aug 11 '16 at 15: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.