# How can I construct a matrix by programming in mathematica?

I want to write a matrix in Mathematica by programming. How can I write it?

The matrix is :

$$M= \begin{bmatrix} 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \end{bmatrix}$$

Considering $$p$$- Number of rows and $$2n+1$$- Number of columns I wrote the following program which is added. But this code is not working. I can form a matrix by matrix tool option. but it is 1000 entries or more that will a problem.

• Take a look at Table.
– Kuba
Oct 30 '18 at 8:28
• If only few nonzero elements are to be assigned, SparseArray provides a short and efficient alternative: Normal[SparseArray[{{1,3}->1},{p,2 n +1 }]] Oct 30 '18 at 8:36
• Essential reading Why I should avoid For loops: mathematica.stackexchange.com/questions/134609/… Oct 30 '18 at 10:19

p = 2; n = 5;
mat = ConstantArray[0, {p, 2 n + 1}];
mat[[1, Floor[n/2 + 1]]] = 1;
mat // MatrixForm // TeXForm


$$\left( \begin{array}{ccccccccccc} 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$$

yourMatrix[p_, n_] := SparseArray[{1, Floor[n/2 + 1]} -> 1, {p, 2 n + 1}]