7
$\begingroup$

I have table of function f[x,a]. For the sake of easy reproduction, let's limit it to a few entries - the original is over 1000 x 1000:

actualtable=Table[f[x, a], {x, 0, 4}, {a, 1, 4}]
*{{f[0, 1], f[0, 2], f[0, 3], f[0, 4]}, {f[1, 1], f[1, 2], f[1, 3], 
f[1, 4]}, {f[2, 1], f[2, 2], f[2, 3], f[2, 4]}, {f[3, 1], f[3, 2], 
f[3, 3], f[3, 4]}, {f[4, 1], f[4, 2], f[4, 3], f[4, 4]}}*

I want to find a way to interject a value m between each list item, to create

desiredtable={{f[0, 1], m, f[0, 2], m, f[0, 3], m, f[0, 4]}, {f[1, 1], m, f[1, 2], 
m, f[1, 3], m, f[1, 4]}, {f[2, 1], m, f[2, 2], m, f[2, 3], m, 
f[2, 4]}, {f[3, 1], m, f[3, 2], m, f[3, 3], m, f[3, 4]}, {f[4, 1], 
m, f[4, 2], m, f[4, 3], m, f[4, 4]}}

I tried using Riffle in conjunction with Table, but it only injects m in between lines, rather than in between individual entries.

Obviously, with a tiny table like this, it's easy, I can just type in the ms, or type the Riffle command once for each line of the output.

But for a very big table, how do I do it?

$\endgroup$

1 Answer 1

8
$\begingroup$

Map Riffle to each of the element of actualTable:

  desiredtable = Riffle[#, "m"] & /@ actualtable

(* {{f[0, 1], "m", f[0, 2], "m", f[0, 3], "m", f[0, 4]}, {f[1, 1], "m", f[1, 2], "m", f[1, 3], "m", f[1, 4]}, {f[2, 1], "m", f[2, 2], "m", f[2, 3], "m", f[2, 4]}, {f[3, 1], "m", f[3, 2], "m", f[3, 3], "m", f[3, 4]}, {f[4, 1], "m", f[4, 2], "m", f[4, 3], "m", f[4, 4]}}*)

$\endgroup$
1
  • $\begingroup$ Perfect. Thanks. $\endgroup$ Sep 16, 2018 at 15:23

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.