5
$\begingroup$

Given a matrix such as:

mat = {{0, 1, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0}, {0, 0, -((g m zg Cos[c] (j0 jz + jz^2 - 2 jz^2 Cos[c]^2 - j0 jz Cos[2 c] + jz^2 Cos[2 c]))/(-j0^2 jz - j0 jz^2 + 2 j0 jz^2 Cos[c]^2 + j0^2 jz Cos[2 c] - j0 jz^2 Cos[2 c])), 0, 0, 0}, {0, 0, 0, 0, 0, 1}, {0, 0, 0, 0, 0, 0}};

$$\left( \begin{array}{cccccc} 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & -\frac{g m \text{zg} \cos (c) \left(-\text{j0} \text{jz} \cos (2 c)-2 \text{jz}^2 \cos ^2(c)+\text{jz}^2 \cos (2 c)+\text{j0} \text{jz}+\text{jz}^2\right)}{\text{j0}^2 \text{jz} \cos (2 c)+2 \text{j0} \text{jz}^2 \cos ^2(c)-\text{j0} \text{jz}^2 \cos (2 c)-\text{j0}^2 \text{jz}-\text{j0} \text{jz}^2} & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 \\ 0 & 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$$

How would one replace the obvious large functional element with a generic latex symbol or simpler functional symbol such as f[a,b,c]

I need to compactly write large similar matrices with massive coupled functions similar to this, but many 1 or 0 elements which I would like to keep. I have naively tried:

mat /. Except[0 | 1, _?NumericQ] -> f[a,b,c]

Which simply replaced every single number and not the the entire functional. In the end I'm looking for:

$$\left( \begin{array}{cccccc} 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & f[a,b,c] & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 \\ 0 & 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$$

$\endgroup$

3 Answers 3

7
$\begingroup$
Replace[mat, Except[0 | 1] -> f[a, b, c], {2}]

TeXForm @ %

$\left( \begin{array}{cccccc} 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & f(a,b,c) & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 \\ 0 & 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$

$\endgroup$
2
$\begingroup$
mat =
  {{0, 1, 0, 0, 0, 0},
   {0, 0, 0, 0, 0, 0},
   {0, 0, 0, 1, 0, 0},
   {0, 0, -(g m zg Cos[c] (j0 jz + jz^2)), 0, 0, 0},
   {0, 0, 0, 0, 0, 1},
   {0, 0, 0, 0, 0, 0}};

Using MapAt:

p = Position[mat, x_ /; Head[x] =!= Integer, {2}, Heads -> False]

(*{{4, 3}}*)

MapAt[F[a, b, c] &, mat, p] // TeXForm

$\left( \begin{array}{cccccc} 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & F(a,b,c) & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 \\ 0 & 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$

$\endgroup$
1
$\begingroup$
mat =
  {{0, 1, 0, 0, 0, 0},
   {0, 0, 0, 0, 0, 0},
   {0, 0, 0, 1, 0, 0},
   {0, 0, -(g m zg Cos[c] (j0 jz + jz^2)), 0, 0, 0},
   {0, 0, 0, 0, 0, 1},
   {0, 0, 0, 0, 0, 0}};

Using ReplaceAt (new in 13.1)

p = First @ Position[mat, x_ /; ! AtomQ[x], {2}]

{4, 3}

ReplaceAt[_ :> f[a, b, c], p] @ mat // MatrixForm

enter image description here

$\endgroup$

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.