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}}
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)$
SparseArray`SparseBlockMatrix
c1 = SparseArray`SparseBlockMatrix[{{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.
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]]