0
$\begingroup$

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

$\endgroup$
0

2 Answers 2

5
$\begingroup$

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...

$\endgroup$
5
  • $\begingroup$ 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}} $\endgroup$
    – user62716
    Commented Feb 3, 2019 at 18:15
  • $\begingroup$ 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 $\endgroup$
    – user62716
    Commented Feb 3, 2019 at 18:30
  • $\begingroup$ Many thanks Henrik, I solved my problem...Best regards $\endgroup$
    – user62716
    Commented Feb 3, 2019 at 19:48
  • $\begingroup$ You're welcome. $\endgroup$ Commented Feb 3, 2019 at 19:56
  • 1
    $\begingroup$ 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}]]]. $\endgroup$ Commented Feb 26, 2019 at 8:16
4
$\begingroup$
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}
$\endgroup$
2
  • $\begingroup$ 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}} $\endgroup$
    – user62716
    Commented Feb 3, 2019 at 18:58
  • $\begingroup$ Many thanks Somos, I solved my problem...Best regards $\endgroup$
    – user62716
    Commented Feb 3, 2019 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.