1
$\begingroup$

I have a matrix in 2x2

a ={{x, y}, {z, u}}

I want to expand this matrix 3x3

b ={{x, y, y}, {z, u, x}, {z, x, u}}

4x4

c ={{x, y, y, y}, {z, u, x, x}, {z, x, u, x}, {z, x, x, u}}

5x5

d ={{x, y, y, y, y}, {z, u, x, x, x}, {z, x, u, x, x}, {z, x, x, u, x}, {z, x, x, x, u}}

and soon...

Basically, I am adding a row and column each time. How to do this automatically, without writing each time?


Edit:

Now consider x, y,z and u are also 2x2 matrices.

Lets

x={{0,1},{1,0}}, y={{1,1},{1,0}}, z={{1,0},{1,1}} 

and

 u = {{p,q},{r,s}}.

How I can get a,b,c and d (defined above) in this case?

$\endgroup$
  • $\begingroup$ At the end you want the resulting matrix to be NxN or NxNx2x2? $\endgroup$ – Kuba Mar 28 '14 at 21:20
5
$\begingroup$

Final answer:

f3[n_] := Module[{m, x, y, z, u},
  m = Normal@SparseArray[Band[{2, 2}] -> u, {n, n}, x];
  m[[2 ;;, 1]] = z;
  m[[1, 2 ;;]] = y;
  x = {{0, 1}, {1, 0}};
  y = {{1, 1}, {1, 0}};
  z = {{1, 0}, {1, 1}};
  u = {{p, q}, {r, s}};
  ArrayFlatten[m]
  ]

f3[6] // MatrixForm

enter image description here


First answer,

For example:

 f[2] = {{x, y}, {z, u}};
 f[n_] := f[n] = ReplacePart[ArrayPad[f[n - 1], {{0, 1}, {0, 1}}, x],
                             {{-1, -1} -> u, {1, -1} -> y, {-1, 1} -> z}]

You may be interested in explanation of f[n_]:=f[n]=... construct

I chose this way becasue you've said you are expanding an array each time adding one row and column. So this is quite nautral approach in such case.

If you are not interested in mediate results, but only in f[2] and f[15] for example, belisarius solution is a way to go.

First response to edit:

second code, more safe approach:

f2[n_] := Module[{m, x, y, z, u},
  m = Nest[ ReplacePart[ ArrayPad[#, {{0, 1}, {0, 1}}, x], 
                         {{-1, -1} -> u, {1, -1} -> y, {-1, 1} -> z}] &, 
            {{x, y}, {z, u}}, n - 2];
  x = {{0, 1}, {1, 0}};
  y = {{1, 1}, {1, 0}};
  z = {{1, 0}, {1, 1}};
  u = {{p, q}, {r, s}};
  ArrayFlatten[m]
  ]

f2[5] // MatrixForm

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ @santosh I've added an explanation that I have not time to do yesterday. $\endgroup$ – Kuba Feb 28 '14 at 7:31
  • $\begingroup$ @kuba...thanks. I was familiar with memorization thing. If I want my matrix a = {{x,x},{x,u}}; to b = {{x, x, x}, {x, u, u}, {x, x, x}} and so on... $\endgroup$ – santosh Mar 14 '14 at 20:36
  • $\begingroup$ @Kuba, you can avoid the tricky requirement that x,y,z,u be Cleared by using substitutions instead of assignment, as in my answer below. $\endgroup$ – evanb Mar 28 '14 at 21:43
  • $\begingroup$ @evanb Good point :) +1. I've provided an alternative way too. It was clearly not safe enough. $\endgroup$ – Kuba Mar 28 '14 at 21:44
4
$\begingroup$
f[n_] := ArrayPad[{{x, y}, {z, x}}, {0, n - 2}, "Fixed"] + 
         DiagonalMatrix[Join[{0}, ConstantArray[u - x, n - 1]]]
f[5] // MatrixForm

Mathematica graphics

f1[n_] := Join[{x}, ConstantArray[y, n - 1]]
frest[n_] := Join[{z}, #] & /@ Permutations[Join[{u}, ConstantArray[x, n - 2]], {n - 1}]
fAll[n_] := Join[{f1[n]}, frest[n]]
MatrixForm[fAll[5]]

Mathematica graphics

| improve this answer | |
$\endgroup$
  • $\begingroup$ If x, y, z, u is itself a 2x2 matrices. Then How to write this form?? $\endgroup$ – santosh Mar 27 '14 at 0:20
3
+25
$\begingroup$

Start with the n x n matrix f[n] given by (for example, belisarius' code):

f[n_] := ArrayPad[{{x, y}, {z, x}}, {0, n - 2}, "Fixed"] + 
     DiagonalMatrix[Join[{0}, ConstantArray[u - x, n - 1]]]

And evaluate

substitutions = {
    x -> {{0, 1}, {1, 0}}, 
    y -> {{1, 1}, {1, 0}}, 
    z -> {{1, 0}, {1, 1}},  
    u -> {{p, q}, {r, s}}
}

If you are looking for a n x n x 2 x 2 object (that is, a matrix whose entries are each 2x2 matrices), then what you want is:

f[n]/.substitutions

(* f[3]/.substitutions

     yields

 {{  { {0, 1}, {1, 0} }, { {1, 1}, {1, 0} }, { {1, 1}, {1, 0} }  },
  {  { {1, 0}, {1, 1} }, { {p, q}, {r, s} }, { {0, 1}, {1, 0} }  }, 
  {  { {1, 0}, {1, 1} }, { {0, 1}, {1, 0} }, { {p, q}, {r, s} }  }} 
*)

while if you are looking for a (2n) x (2n) matrix where each entry is a number, then likely what you desire is

ArrayFlatten[ f[n]/.substitutions ]

(* ArrayFlatten[ f[3]/.substitutions ]

    yields

{{0, 1, 1, 1, 1, 1}, 
 {1, 0, 1, 0, 1, 0}, 
 {1, 0, p, q, 0, 1},
 {1, 1, r, s, 1, 0},
 {1, 0, 0, 1, p, q},
 {1, 1, 1, 0, r, s}}
*)
| improve this answer | |
$\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.