Given that I have a vector and matrix as follow:

vec = Array[Subscript["P", #] &, 6, 0];
mat = Array[Subscript["P", #1, #2] &, {5, 4}, {0, 0}];


And I could use the built-in ArrayPad[] to achieve the below style:

ArrayPad[vec , 1, "Fixed"] // MatrixForm


However, when the $$P_i$$ or $$P_{i,j}$$ is a coordinate of 2D and 3D point, respesctively, this method will in fail.

For instance,

vec1 = {{1, 1}, {2, 3}, {3, -1}, {4, 1}, {5, 0}};

(* {{1, 1, 1, 1}, {1, 1, 1, 1}, {2, 2, 3, 3},
{3, 3, -1, -1}, {4, 4, 1, 1}, {5, 5, 0, 0}, {5, 5, 0, 0}}*)


In fact, I need

{{1, 1}, {1, 1}, {2, 3}, {3, -1}, {4, 1}, {5, 0}, {5,0}}


rather than

{{1, 1, 1, 1}, {1, 1, 1, 1}, {2, 2, 3, 3},
{3, 3, -1, -1}, {4, 4, 1, 1}, {5, 5, 0, 0}, {5, 5, 0, 0}}


An alternative method that I could figure out is using Hold[] and ReleaseHold[].

vec2 = Hold/@{{1, 1}, {2, 3}, {3, -1}, {4, 1}, {5, 0}};
(*{{1, 1}, {1, 1}, {2, 3}, {3, -1}, {4, 1}, {5, 0}, {5, 0}}*)

mat2 =
{{{1, 1, 0}, {1, 2, 1}, {1, 3, 1}},
{{2, 1, 1}, {2, 2, -1}, {2,3, 1}},
{{3, 1, 1}, {3, 2, 1}, {3, 3, -1}},
{{4, 1, 1}, {4, 2, -1}, {4, 3, 0}}};
ArrayPad[Map[Hold, mat2, {2}], 1, "Fixed"] // ReleaseHold

ArrayPad[Map[Hold, mat2, {2}], 1, "Fixed"] // MatrixForm
(*
{{{1, 1, 0}, {1, 1, 0}, {1, 2, 1}, {1, 3, 1}, {1, 3, 1}},
{{1, 1, 0}, {1, 1, 0}, {1, 2, 1}, {1, 3, 1}, {1, 3, 1}},
{{2, 1, 1}, {2, 1, 1}, {2, 2, -1}, {2, 3, 1}, {2, 3, 1}},
{{3, 1, 1}, {3, 1, 1}, {3, 2, 1}, {3, 3, -1}, {3, 3, -1}},
{{4, 1, 1}, {4, 1, 1}, {4, 2, -1}, {4, 3, 0}, {4, 3, 0}},
{{4, 1, 1}, {4, 1, 1}, {4, 2, -1}, {4, 3, 0}, {4, 3, 0}}}
*)


So my question is how to achieve the right extension of matrix/vector using ArrayPad[...,"Fixed"] or other good solution.

• Try e.g. ArrayPad[vec1, {1}, "Fixed"] – Mike Honeychurch Dec 9 '15 at 7:20
• @MikeHoneychurch En, how about understand this usage {1}, and how to extend this to matrix? – xyz Dec 9 '15 at 7:24
• please see my edited answer. it also works for matrices – Mike Honeychurch Dec 9 '15 at 7:26

vec1 = {{1, 1}, {2, 3}, {3, -1}, {4, 1}, {5, 0}};

(* {{1, 1}, {1, 1}, {2, 3}, {3, -1}, {4, 1}, {5, 0}, {5, 0}}  *)


or

ArrayPad[vec1, {{1, 1}}, "Fixed"]


which is more in keeping with the docs. For matrices:

mat2 = {{{1, 1, 0}, {1, 2, 1}, {1, 3, 1}}, {{2, 1, 1}, {2, 2, -1}, {2,
3, 1}}, {{3, 1, 1}, {3, 2, 1}, {3, 3, -1}}, {{4, 1, 1}, {4,
2, -1}, {4, 3, 0}}};


or

ArrayPad[mat2, {{1, 1}, {1, 1}}, "Fixed"]


which seems to be more consistent with the docs (on my reading of them anyway).

For the matrix, according the the question, you want to pad on both ends and both sides, therefore you need to use 2 specifications. On top of that you need to use lists due to the extra level relative to vectors.

Just to add to Mike's answer, the relevant part of the help for ArrayPad[] says

$\tt ArrayPad$[array,{{$m_1$ , $n_1$},{$m_2$ , $n_2$},…},…] pads with $m_i$, $n_i$ elements at level $i$ in array.

So when you don't give the padding in the form of a list it applies that padding to every single level specification.

Since your array has $3$ levels, there are three different ways you could pad $1$ element to a single level,

{ArrayPad[mat2, 1, "Fixed"] // MatrixForm,
ArrayPad[mat2, {{1}, {0}, {0}}, "Fixed"] // MatrixForm,
ArrayPad[mat2, {{0}, {1}, {0}}, "Fixed"] // MatrixForm,
ArrayPad[mat2, {{0}, {0}, {1}}, "Fixed"] // MatrixForm}


• Thanks a lot for your detailed explanation:) – xyz Dec 9 '15 at 7:44