0
$\begingroup$

I want to do padding of matrix

a = {{x,x},{x,u}}; 

to

b = {{x, x, x}, {x, u, u}, {x, x, x}}

c = {{x, x, x, x}, {x, u, u, u}, {x, x, x, x}, {x, x, x, x}}

and soon...

How I can automatically do this?

$\endgroup$
5
  • $\begingroup$ You can use ArrayFlatten. $\endgroup$ – Szabolcs Mar 16 '14 at 22:39
  • 2
    $\begingroup$ Do all subsequent elements have only THREE rows? $\endgroup$ – Dr. belisarius Mar 16 '14 at 22:58
  • 2
    $\begingroup$ Isn't it very similar to your other question? mathematica.stackexchange.com/q/43071/193 $\endgroup$ – Dr. belisarius Mar 16 '14 at 23:00
  • $\begingroup$ @Kuba In fact the OP added a third element showing that the result should always be a square matrix. He hasn't be very outspoken, though. The problem I find with this question is that the OP already asked one of these (where your answer is quite good) but isn't trying with the hints given there (ArrayPad, etc) $\endgroup$ – Dr. belisarius Mar 18 '14 at 16:42
  • $\begingroup$ @belisarius If you are ok with that then I should be too :P Yes, that's often a problem, their loss if they don't want to understand the code :| $\endgroup$ – Kuba Mar 18 '14 at 16:49
2
$\begingroup$

I must confess to being confused about the ultimate aim. I, therefore, am sorry if these speculations miss the mark.

If the aim is to progressively pad right with the last element of every list then:

g[s_, n_] := PadRight[#, n, Last@#] & /@ s

Repeatedly applying:

Column[Table[g[a, j], {j, 3, 6}]]

yields:

enter image description here

If the intent is to sequential pad right with the first element then pad right with the last element of each subset then:

f[s_] := With[{w = First[s]}, 
   ReleaseHold[PadRight[s, Length[s] + 1, Hold[w]]]];

You can get lists:

FoldList[g[f[#1], #2] &, a, Range[3, 6]]

enter image description here

If the aim is just to expand first list then apply g:

Table[g[f[a], j], {j, 3, 6}]

enter image description here

The latter seems to be consistent with outcome presented. However, I may have misunderstood intent.

$\endgroup$
2
  • $\begingroup$ how to write only this out of full fold list: {{x, x, x, x}, {x, u, u, u}, {x, x, x, x}, {x, x, x, x}} $\endgroup$ – santosh Mar 17 '14 at 23:00
  • $\begingroup$ Thanks...I want this....FoldList[g[f[#1], #2] &, a, Range[3, 6]][[n]] // MatrixForm, n=1,2,3... $\endgroup$ – santosh Mar 17 '14 at 23:04
3
$\begingroup$

You might use:

a = {{x, x}, {x, u}};

{#, #2, #} & @@ ArrayPad[a, {0, {0, 3}}, "Fixed"]
{{x, x, x, x, x}, {x, u, u, u, u}, {x, x, x, x, x}}
$\endgroup$
2
  • 1
    $\begingroup$ LOL - this was flagged as a "low quality post" - I guess the clever filter is broken. $\endgroup$ – ciao Mar 17 '14 at 2:06
  • $\begingroup$ @Mr.Wizard thank you again for educating...ArrayPad. I am still uncertain about ultimate aim. $\endgroup$ – ubpdqn Mar 17 '14 at 3:58
1
$\begingroup$
base = {{x, x}, {x, u}, {x, x}}

pads = Rest@NestList[Function[arg, PadRight[#, Length@# + 1, Last@#] & /@ arg],base, 4];

Each entry of pads is one more level of padding.

Change the 4 in the NestList to however many different levels of padding you want produced.

Perhaps a bit "cleaner", same results:

Rest@NestList[ArrayPad[#, {{0, 0}, {0, 1}}, "Fixed"] &, base, 4]
$\endgroup$

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.