5
$\begingroup$

consider a matrix

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

How do I do a program that does these steps

1-Replace each element equal to 1 by $x_{ij}$

2- Replace elements on the diagonal by 1

After execution the matrix will be formed

$\left( \begin{array}{ccccc} 1 & 0 & x_{1,3} \\ x_{2,1} & 1 & 0 \\ x_{3,1} & x_{3,2} & 1 \\ \end{array}\right)$

My attempts with the function

ReplacePart[MM, {i,j} -> x_] , If and MM[[i,j]]=new

But I did not come out with a result, knowing that I am beginning in Mathematica

$\endgroup$

6 Answers 6

6
$\begingroup$
MapIndexed[If[Equal @@ #2, 1, # Subscript[x, ##& @@ #2]]&, #, {2}]&@
   {{0, 0, 1}, {1, 0, 0}, {1, 1, 0}};

% // TeXForm

$\left( \begin{array}{ccc} 1 & 0 & x_{1,3} \\ x_{2,1} & 1 & 0 \\ x_{3,1} & x_{3,2} & 1 \\ \end{array} \right)$

MapIndexed[f[Equal @@ #2, 1, # Subscript[x, ## & @@ #2]] &, #, {2}] &@
    RandomInteger[1, {10, 10}] // TeXForm

$\left( \begin{array}{ccccccccc} 1 & x_{1,2} & 0 & x_{1,4} & x_{1,5} & 0 & x_{1,7} & 0 & x_{1,9} \\ 0 & 1 & x_{2,3} & 0 & x_{2,5} & x_{2,6} & 0 & x_{2,8} & x_{2,9} \\ 0 & x_{3,2} & 1 & 0 & x_{3,5} & 0 & 0 & x_{3,8} & 0 \\ 0 & x_{4,2} & 0 & 1 & x_{4,5} & 0 & 0 & 0 & 0 \\ 0 & 0 & x_{5,3} & 0 & 1 & 0 & x_{5,7} & x_{5,8} & 0 \\ x_{6,1} & 0 & 0 & 0 & 0 & 1 & x_{6,7} & 0 & 0 \\ 0 & 0 & x_{7,3} & x_{7,4} & 0 & 0 & 1 & x_{7,8} & 0 \\ x_{8,1} & x_{8,2} & 0 & 0 & 0 & x_{8,6} & x_{8,7} & 1 & x_{8,9} \\ 0 & x_{9,2} & 0 & x_{9,4} & x_{9,5} & 0 & x_{9,7} & 0 & 1 \\ \end{array} \right) $

$\endgroup$
9
  • $\begingroup$ Thank you very much. Can this code be in general? Applied to matrix of size nxn $\endgroup$
    – user44376
    Commented Jul 11, 2017 at 22:07
  • $\begingroup$ @Emad, yes it can be used for a matrix of any size. $\endgroup$
    – kglr
    Commented Jul 11, 2017 at 22:10
  • $\begingroup$ @EmadKreem it is always nice to upvote answers ! $\endgroup$
    – Ali Hashmi
    Commented Jul 11, 2017 at 22:27
  • $\begingroup$ @kglr if a matrix in MatrixForm Do not get a result $\endgroup$
    – user44376
    Commented Jul 11, 2017 at 22:38
  • $\begingroup$ +1 ! for nice use of MapIndexed $\endgroup$
    – Ali Hashmi
    Commented Jul 11, 2017 at 22:43
5
$\begingroup$

As the diagonals of mat are zero, we need only multiply mat by a subscripted array and add an identity matrix:

(mat Array[Subscript[x, ##] &, {3, 3}]) + IdentityMatrix[3] // MatrixForm  
(*Thanks to David G Stork for IdentityMatrix*)

$$ \left( \begin{array}{ccc} 1 & 0 & x_{1,3} \\ x_{2,1} & 1 & 0 \\ x_{3,1} & x_{3,2} & 1 \\ \end{array} \right) $$

More generally, if the diagonals in mat are not zero (and using ciao's answer to this question):

mat2 // UpperTriangularize[#, 1] + LowerTriangularize[#, -1] & 
// ((# Array[Subscript[x, ##] &, {3, 3}]) + IdentityMatrix[3]) & 

where:

mat = {{0, 0, 1}, {1, 0, 0}, {1, 1, 0}}
mat2 = {{6, 0, 1}, {1, 6, 0}, {1, 0, 6}}
$\endgroup$
2
  • 1
    $\begingroup$ Minor suggestion: replace DiagonalMatrix[{1,1,1}] by IdentityMatrix[3]. $\endgroup$ Commented Jul 12, 2017 at 1:28
  • $\begingroup$ @ David G. Stork Thanks! (have modified answer as you suggested) $\endgroup$
    – user1066
    Commented Jul 12, 2017 at 1:51
4
$\begingroup$
m = {{0, 0, 1}, {1, 0, 0}, {1, 1, 0}};

r = # -> Subscript[x, Sequence @@ #] & /@ Position[m, 1];

mn = ReplacePart[m, Append[r, {i_, i_} -> 1]];

mn // MatrixForm

enter image description here

Or

f[m_, i_, j_] := 0
f[m_, i_, i_] := 1
f[m_, i_, j_] /; m[[i, j]] == 1 := Subscript[x, i, j]

mn = Table[f[m, i, j], {i, Length@m}, {j, Length@m}];
$\endgroup$
6
  • $\begingroup$ I imposed that matrix m but I did not get the result $\endgroup$
    – user44376
    Commented Jul 11, 2017 at 21:54
  • $\begingroup$ Try again with m evaluated (first line) $\endgroup$
    – eldo
    Commented Jul 11, 2017 at 21:58
  • $\begingroup$ Thank you very much. Can this code be in general? Applied to matrix of size nxn $\endgroup$
    – user44376
    Commented Jul 11, 2017 at 22:02
  • $\begingroup$ It should function for any square matriv $\endgroup$
    – eldo
    Commented Jul 11, 2017 at 22:10
  • $\begingroup$ +1 ! thought of the exact same solution. got scooped :) $\endgroup$
    – Ali Hashmi
    Commented Jul 11, 2017 at 22:44
3
$\begingroup$
changeMatrix[mat_] := Module[{dim = Length@mat},
  Normal@SparseArray[
    Prepend[
      First /@ Most@ArrayRules@mat /. {a__?NumericQ} :> ({a} -> Subscript[x, a]),
      {i_, i_} -> 1
     ],
    {dim, dim}
   ]
  ]

If, for some reason, your matrices are wrapped in MatrixForm (they shouldn't be! -- this should only be used for display purposes), then one can add the following definition:

changeMatrix[MatrixForm[mat_]] := changeMatrix[mat] // MatrixForm
$\endgroup$
4
  • $\begingroup$ if a mat in MatrixForm Do not get a result $\endgroup$
    – user44376
    Commented Jul 11, 2017 at 22:39
  • $\begingroup$ +1 ! the approach here creates a matrix using the rules formed from the supplied matrix. You should not use MatrixForm anyways since it is generally reserved for display purposes and not matrix manipulation $\endgroup$
    – Ali Hashmi
    Commented Jul 11, 2017 at 22:43
  • $\begingroup$ How I can convert matrixform to form $\endgroup$
    – user44376
    Commented Jul 11, 2017 at 22:48
  • $\begingroup$ @march Thank you so much. I wish you success and excellence forever $\endgroup$
    – user44376
    Commented Jul 11, 2017 at 23:11
2
$\begingroup$
m = {{0, 0, 1}, {1, 0, 0}, {1, 1, 0}};

Using ReplacePart and MapIndexed

ReplacePart[{i_, i_} :> 1] @
  MapIndexed[If[#1 == 1, Subscript[x, Sequence @@ #2], #1] &, m, {2}] // MatrixForm

enter image description here

$\endgroup$
2
$\begingroup$

Using Switch to define your rules:

rulesFunc[elem_, {i_, j_}] := Switch[{elem, i == j}, {_, True}, 1, {1, False}, 
                              Subscript[x, i, j], _, elem ]

Finally, using ReplacePart:

ReplacePart[m, {i_, j_} :> rulesFunc[m[[i, j]], {i, j}]] // TeXForm

$\left( \begin{array}{ccc} 1 & 0 & x_{1,3} \\ x_{2,1} & 1 & 0 \\ x_{3,1} & x_{3,2} & 1 \\ \end{array} \right)$

$\endgroup$

Your Answer

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