# Replace element of a matrix with condition

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

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)$

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

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 // 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) &


where:

mat = {{0, 0, 1}, {1, 0, 0}, {1, 1, 0}}
mat2 = {{6, 0, 1}, {1, 6, 0}, {1, 0, 6}}

• Minor suggestion: replace DiagonalMatrix[{1,1,1}] by IdentityMatrix. – David G. Stork Jul 12 '17 at 1:28
• @ David G. Stork Thanks! (have modified answer as you suggested) – user1066 Jul 12 '17 at 1:51
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 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}];

• I imposed that matrix m but I did not get the result – user44376 Jul 11 '17 at 21:54
• Try again with m evaluated (first line) – eldo Jul 11 '17 at 21:58
• Thank you very much. Can this code be in general? Applied to matrix of size nxn – user44376 Jul 11 '17 at 22:02
• It should function for any square matriv – eldo Jul 11 '17 at 22:10
• +1 ! thought of the exact same solution. got scooped :) – Ali Hashmi Jul 11 '17 at 22:44
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

• if a mat in MatrixForm Do not get a result – user44376 Jul 11 '17 at 22:39
• +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 – Ali Hashmi Jul 11 '17 at 22:43
• How I can convert matrixform to form – user44376 Jul 11 '17 at 22:48
• @march Thank you so much. I wish you success and excellence forever – user44376 Jul 11 '17 at 23:11