3
$\begingroup$

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
ArrayPad[mat, 1, "Fixed"] // MatrixForm

enter image description here

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}};
ArrayPad[vec1, 1, "Fixed"]

(* {{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}};
ArrayPad[vec2, 1, "Fixed"] // ReleaseHold
(*{{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}}}
*)

enter image description here


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

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

(* {{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}}};
ArrayPad[mat2, {{1}, {1}}, "Fixed"]

or

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

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

enter image description here

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.

$\endgroup$
2
$\begingroup$

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}

enter image description here

$\endgroup$
1
  • $\begingroup$ Thanks a lot for your detailed explanation:) $\endgroup$ – xyz Dec 9 '15 at 7:44

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.