# Joining matrices together

Given the following matrices (where $$n$$ can be any integer number, It is just for a general dimension of my space):

n = 5;
A = RandomInteger[{1, 9}, {1, 1}]
B = RandomInteger[{1, 9}, {1, n - 1}]
F = RandomInteger[{1, 9}, {n - 1, n - 1}]


I want to have a full-matrix $$c$$ defined as:

$$c = \begin{pmatrix} A & B \\ B & F \end{pmatrix}.$$

I have tried using Join and ArrayFlatten, however I cannot do it.

Besides this, how could I impose that $$F$$ is a symmetric integer-random matrix?

## 3 Answers

SeedRandom[1]
n = 5;
A = RandomInteger[{1, 9}, {1, 1}];
B = RandomInteger[{1, 9}, {1, n - 1}];
F = RandomInteger[{1, 9}, {n - 1, n - 1}];

{A, B, F}


{{{2}},
{{5, 1, 8, 1}},
{{1, 9, 7, 1}, {5, 2, 9, 6}, {2, 2, 2, 4}, {3, 2, 7, 1}}

### ArrayFlatten

You can use Transpose@B in the second block and ArrayFlatten:

c = ArrayFlatten[{{A, B}, {Transpose @ B, F}}]

TeXForm @ MatrixForm @ c


$$\left( \begin{array}{ccccc} 2 & 5 & 1 & 8 & 1 \\ 5 & 1 & 9 & 7 & 1 \\ 1 & 5 & 2 & 9 & 6 \\ 8 & 2 & 2 & 2 & 4 \\ 1 & 3 & 2 & 7 & 1 \\ \end{array} \right)$$

### SparseArraySparseBlockMatrix

c1 =  SparseArraySparseBlockMatrix[{{1, 1} -> A, {1, 2} -> B,
{2, 1} -> Transpose @ B, {2, 2} -> F}]

Normal @ c1 == c


True

See also: this answer by OlexandrR and this.

### ArrayReshape

To use ArrayReshape we need to process the second row block as follows:

c2 = ArrayReshape[{A, B, Transpose @ {Transpose @ B, F}}, {n, n}]

c2 == c


True

Note: to see why we need the more complicated form to use ArrayReshape make B a symbolic matrix

B2 = Array[b, {1, n - 1}];


and compare

TeXForm @ MatrixForm @ ArrayReshape[{A, B2, Transpose @ {Transpose @ B2, F}}, {n, n}]


$$\left( \begin{array}{ccccc} 2 & b(1,1) & b(1,2) & b(1,3) & b(1,4) \\ b(1,1) & 1 & 9 & 7 & 1 \\ b(1,2) & 5 & 2 & 9 & 6 \\ b(1,3) & 2 & 2 & 2 & 4 \\ b(1,4) & 3 & 2 & 7 & 1 \\ \end{array} \right)$$

with what you get with the simpler/ more elegant form:

TeXForm @ MatrixForm @ ArrayReshape[{{A, B2}, {B2, F}}, {n, n}]


$$\left( \begin{array}{ccccc} 2 & b(1,1) & b(1,2) & b(1,3) & b(1,4) \\ b(1,1) & b(1,2) & b(1,3) & b(1,4) & 1 \\ 9 & 7 & 1 & 5 & 2 \\ 9 & 6 & 2 & 2 & 2 \\ 4 & 3 & 2 & 7 & 1 \\ \end{array} \right)$$

Update: A function to construct random symmetric integer matrices:

ClearAll[ranSymIntMat]
ranSymIntMat[range_, d_] := Symmetrize[2 #] - IdentityMatrix[d] # & @
UpperTriangularize[RandomInteger[range, {d, d}]]


Examples:

SeedRandom[1]
Row[MatrixForm @ ranSymIntMat[9, #] & /@ Range[2, 7], Spacer[10]]


Something like this?

ArrayReshape[{{A, B}, {B, F}}, {n, n}]
MatrixForm@%


{{9, 6, 9, 3, 6}, {6, 9, 3, 6, 7}, {3, 8, 2, 8, 2}, {5, 5, 2, 8, 8}, {3, 1, 9, 2, 1}}

$$\left( \begin{array}{ccccc} 9 & 6 & 9 & 3 & 6 \\ 6 & 9 & 3 & 6 & 7 \\ 3 & 8 & 2 & 8 & 2 \\ 5 & 5 & 2 & 8 & 8 \\ 3 & 1 & 9 & 2 & 1 \\ \end{array} \right)$$

Besides this, how could I impose that $$F$$ is a symmetric integer-random matrix?

Here is one way to do it.

SeedRandom@2
n = 5;
H = PadRight[
TakeList[RandomInteger[{1, 9}, n^2], Range[n - 1]], {n - 1,
n - 1}];
F = H + Transpose@H - DiagonalMatrix@Diagonal@H


$$\left( \begin{array}{cccc} 9 & 5 & 5 & 1 \\ 5 & 6 & 8 & 2 \\ 5 & 8 & 5 & 1 \\ 1 & 2 & 1 & 5 \\ \end{array} \right)$$

Or use the answer here

  H = RandomInteger[{1, 9}, {n - 1, n - 1}];
upper = UpperTriangularize[H, 1];
diag = DiagonalMatrix[Diagonal@H];
F=diag + upper + Transpose[upper]


Or this

 H = RandomInteger[{1, 9}, {n - 1, n - 1}];
lower = LowerTriangularize[H];
diag = DiagonalMatrix[Diagonal@H];
F = lower + Transpose[lower] - diag