0
$\begingroup$

Consider the following:

PadLeft[{{a,b},{c,d}},{2,2}+{0,1},Transpose@{{1,2}}] 

{{1, a, b}, {2, c, d}}

PadLeft[{{a,b},{c,d}},{2,2}+{0,2},Transpose@{{1,2},{3,4}}] 

{{1, 3, a, b}, {2, 4, c, d}}

So far padding of additional columns is happening correctly in order. Now consider the following:

PadLeft[{{a,b},{c,d}},{2,2}+{0,3},Transpose@{{1,2},{3,4},{5,6}}] 

{{3, 5, 1, a, b}, {4, 6, 2, c, d}}

What happened here? I was expecting the following:

{{1, 3, 5, a, b}, {2, 4, 6, c, d}}

I was relying upon this and now this is messing up my work.

$\endgroup$

1 Answer 1

2
$\begingroup$

Per the documentation:

With padding {x1, x2,...,xs}, cyclic repetitions of the xi are effectively laid down and then the list is superimposed on top of them, with the last element of the list lying on an occurrence of xs.

So, since you're padding left, the padding is laid down so that the right edges of the padding and your list are aligned. Try padding an empty structure to see how the padding works:

PadLeft[{{}}, {2, 4}, {{1, 3}, {2, 4}}] (* {{1, 3, 1, 3}, {2, 4, 2, 4}} *) (* So, it just coincidentally looks like it would if padding was aligned along the left edge. *) PadLeft[{{}}, {2, 5}, {{1, 3, 5}, {2, 4, 6}}] (* {3, 5, 1, 3, 5}, {4, 6, 2, 4, 6} *) (* Here you can see how the alignment works. *) 

So, the fix is to specify your padding so that it aligns along the right edge in such a way to achieve your desired result. Or if that's not feasible, use something other than PadLeft. Maybe a Join or some other structural manipulation. Or, you could actually use PadRight:

PadRight[{{a, b}, {c, d}}, {2, 5}, {{1, 3, 5}, {2, 4, 6}}, {0, 3}] (* {{1, 3, 5, a, b}, {2, 4, 6, c, d}} *) 
$\endgroup$
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.