1
$\begingroup$

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.

enter image description here

$\endgroup$
3
  • $\begingroup$ Take a look at Table. $\endgroup$
    – Kuba
    Oct 30 '18 at 8:28
  • 1
    $\begingroup$ 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 }]] $\endgroup$ Oct 30 '18 at 8:36
  • 2
    $\begingroup$ Essential reading Why I should avoid For loops: mathematica.stackexchange.com/questions/134609/… $\endgroup$ Oct 30 '18 at 10:19
1
$\begingroup$
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)$

$\endgroup$
0
$\begingroup$

How about this

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

yourMatrix[2, 5] // MatrixForm
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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