4
$\begingroup$

I would like to pad a nested list like

M = {{{a, b}, {c, d}}, {{e, f}, {g, h}}}

enter image description here

with some constant "vector", e.g. {0, 0}. In that case the desired result would be

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

enter image description here

However, I somehow can't figure out how to get ArrayPad to do what I want directly. My workaround

(ArrayPad[M, {{1}, {1}}, foo]) /. foo -> {0, 0}

is not pretty and I want to get rid of it. Surely, this can be done with ArrayPad alone!

$\endgroup$
  • 5
    $\begingroup$ ArrayPad[m, {{1, 1}, {1, 1}}, {{{0, 0}}}] $\endgroup$ – ciao Jun 24 '16 at 22:52
  • 3
    $\begingroup$ or ArrayPad[m, {{1}, {1}}, {{{0, 0}}}] if every keystroke counts...Also, I neglected to note in first comment - avoid using uppercase initials for your symbols like the plague - you risk clashing with built-ins... $\endgroup$ – ciao Jun 24 '16 at 23:51
  • 3
    $\begingroup$ Treating your input as a rank-3 tensor: ArrayPad[{{{a, b}, {c, d}}, {{e, f}, {g, h}}}, {1, 1, 0}, 0] Replace the third argument with any constant you want. $\endgroup$ – J. M. will be back soon Jun 25 '16 at 0:51
  • 1
    $\begingroup$ Thank you all! Sorry for the trivial question, but I just couldn't figure it out... $\endgroup$ – murphy Jun 25 '16 at 9:23

Browse other questions tagged or ask your own question.