# Create special matrix

I need to construct special matrix in mathematica which is given in the following image

The Array command provides a simple way to transform a function into a matrix.

n = 6;
η = ConstantArray[1, n+1];
η[[1]] = 2;
a = Array[{i, j} \[Function] UnitStep[i - j] Mod[i - j, 2] 4 (i - 1)/η[[j]], {n+1, n+1}];
a // TeXForm


$$\left( \begin{array}{ccccccc} 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 2 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 8 & 0 & 0 & 0 & 0 & 0 \\ 6 & 0 & 12 & 0 & 0 & 0 & 0 \\ 0 & 16 & 0 & 16 & 0 & 0 & 0 \\ 10 & 0 & 20 & 0 & 20 & 0 & 0 \\ 0 & 24 & 0 & 24 & 0 & 24 & 0 \\ \end{array} \right)$$

Is this what you are looking for? Zero-based indexing is quite untypical in mathematics...

• Thanks sir, the matrix should be:{{0, 0, 0, 0, 0, 0, 0}, {2, 0, 0, 0, 0, 0, 0}, {0, 8, 0, 0, 0, 0, 0}, {6, 0, 12, 0, 0, 0, 0}, {0, 16, 0, 16, 0, 0, 0}, {10, 0, 20, 0, 20, 0, 0}, {0, 24, 0, 24, 0, 24, 0}} Commented Feb 3, 2019 at 18:15
• Dear, thanks again for fast response, I try to do it:d = Table[ Which[j == i - 2, 0, j == i - 4, 0, j == i - 6, 0, j == i - 8, 0, j == i - 10, 0, j == i - 12, 0, j == i - 14, 0, j >= i, 0, j == 0, 2*i, i > j, 4*i], {i, 0, n}, {j, 0, n}] but this will work till n=14, remember please I,j=0.....n so when n=6 it will be 7x7 .......best regards Commented Feb 3, 2019 at 18:30
• Many thanks Henrik, I solved my problem...Best regards Commented Feb 3, 2019 at 19:48
• You're welcome. Commented Feb 3, 2019 at 19:56
• SparseArray[] can also be used: With[{n = 6}, Normal[SparseArray[{i_, j_} /; i > j && OddQ[i - j] :> 4 (i - 1)/(2 - UnitStep[j - 2]), {n + 1, n + 1}]]]. Commented Feb 26, 2019 at 8:16
dij[i_, j_] := If[i < j, 0, Mod[i - j, 2]] 4 i / If[j == 0, 2, 1];
d[n_] := Table[dij[i, j], {i, 0, n}, {j, 0, n}];


The function d[] returns a Mathematica matrix as a list of lists as you would expect. For exmaple, the following code returns True.

d[3] == {{0, 0, 0, 0}, {2, 0, 0, 0}, {0, 8, 0, 0}, {6, 0, 12, 0}}


A bigger example is the code d[6] // Column which returns each matrix row on a different line.

{0, 0, 0, 0, 0, 0, 0}
{2, 0, 0, 0, 0, 0, 0}
{0, 8, 0, 0, 0, 0, 0}
{6, 0, 12, 0, 0, 0, 0}
{0, 16, 0, 16, 0, 0, 0}
{10, 0, 20, 0, 20, 0, 0}
{0, 24, 0, 24, 0, 24, 0}

• Dear Somos, thank you for your perfect reponse, can I make the output like:{{0, 0, 0, 0, 0, 0, 0}, {2, 0, 0, 0, 0, 0, 0}, {0, 8, 0, 0, 0, 0, 0}, {6, 0, 12, 0, 0, 0, 0}, {0, 16, 0, 16, 0, 0, 0}, {10, 0, 20, 0, 20, 0, 0}, {0, 24, 0, 24, 0, 24, 0}} Commented Feb 3, 2019 at 18:58
• Many thanks Somos, I solved my problem...Best regards Commented Feb 3, 2019 at 19:46