# How to build a matrix of size n + 4 from a matrix of size n and 4 rows and 4 columns?

I have a matrix size n + 4 very long and very complicated and to find my matrix I have a matrix of size n and 3 rows and 3 columns To better understand my problem here is a simple example

matrix = ParallelTable[AAA[i, j], {i, 1, nnn, 1}, {j, 1, nnn, 1}];
Do[(A[i, p] = matrix[[i, p]]), {i, 1, nnn, 1}, {p, 1, nnn, 1}];

rows1 = ParallelTable [BB[i, j], {i, 1, nnn, 1}];
Do [A[nnn + 1, i] = rows1[[i]], {i, 1, nnn, 1}];
rows2 = ParallelTable [CCC[i, j], {i, 1, nnn, 1}];
Do [A[nnn + 2, i] = rows2[[i]], {i, 1, nnn, 1}];
rows3 = ParallelTable [DDD[i, j], {i, 1, nnn, 1}];
Do [A[nnn + 3, i] = rows3[[i]], {i, 1, nnn, 1}];

columns1 = ParallelTable [EEE[i, j], {i, 1, nnn, 1}];
Do [A[i, nnn + 1] = columns1[[i]], {i, 1, nnn, 1}];

columns2 = ParallelTable [FFF[i, j], {i, 1, nnn, 1}];
Do [A[i, nnn + 2] = columns2[[i]], {i, 1, nnn, 1}];

columns3 = ParallelTable [GGG[i, j], {i, 1, nnn, 1}];
Do [A[i, nnn + 3] = columns3[[i]], {i, 1, nnn, 1}];
Table [A[i, j], {i, 1, nnn + 3, 1}, {p, 1, nnn + 3, 1}];


For the moment I built my matrix like that, it works but it takes time

i trry to use

Table [{matrix, rows1, rows2, rows3,...}];


it's doesn't work

sorry for my english

• Try ?ArrayFlatten – chris Jun 10 at 15:53
• Try using SparseArray with Band. From the documentation: SparseArray[{Band[{1, 1}] -> x, Band[{2, 1}] -> y}, {5, 5}]  – Eric William Smith Jun 10 at 16:18
• I tried this but get an error: SparseArray::bndtr: There are insufficient positions available in the Band[{1,1},{5,5},{1,1}] to fit all the values {{{3}},{{1}},{{3}},{{5}},{{3}},{{1}},{{5}}} so some of the values will be omitted in the specification Band[{1,1},{5,5},{1,1}]->{{{3}},{{1}},{{3}},{{5}},{{3}},{{1}},{{5}}}. It does produce a result but that isn't what the documentation shows. – Mark R Jun 10 at 20:03
• And to be clear, I know that you are using the example from the documentation - that expression is what I'm saying has an error when you evaluate. – Mark R Jun 10 at 20:13

One way of extending an nXn array by 4 more rows and 4 columns is to do the following (ArrayPad extends on all sides so that is why we need to use 4/2=2):

startArray = ConstantArray[1, {10, 10}];
newArray =
RotateLeft[ArrayPad[startArray, 2, 0], {2,2}];
newArray // MatrixForm


You then need to assign the newArray values with whatever means you want. If you have a complete row (with n+4 elements), you may use this:

newArray[[12]] = Range[21, 34];


Or a complete column could be changed like so:

newArray[[All, 11]] = Table[-1, {14}];


Obviously, "Range[31,34]" and "Table[-1, {14}]" are placeholders for whatever row or column you wish to replace.